English 中文(简体)
如何在jquery访问附带的事件处理器?
原标题:How to access the attached event handler in jquery?

我在HTML里有一个简单的按钮,像这样:

<input type= button  id= btnPrint  value= Print  onclick="changeStylesheet();" />

此按钮已经包含一个点击事件处理器 。 我试图在此实现的是能够为相同的按钮拼写点击事件处理器 。 我想添加第二个方法, 来跟踪按钮被点击的次数 。

<强 > 某些方法:

First: Append another function call in HTML as per inline event registration.

<input type= button  id= btnPrint  value= Print  onclick= changeStylesheet();trackThis();>  />

这是我最容易走的路,但对我来说不可行。

第二: 抓取单击事件并检查它是否未定义。 然后, 用 JavaScript 本地语法将事件处理器重新组合为匿名功能 。

var elem = document.getElementById( btnPrint );
var existingHandler = **elem.onclick ? elem.onclick : function () {};**
elem.onclick = function () {
     trackThis();
     existingHandler();
};

第三:尽管以上方法有效,但我很想知道如何在2号方法上找到方法,但对jQuery语法却有所了解。

 var elem = $( #btnPrint );
    var existingHandler = **elem.click ? elem.click : function () {};**
    elem.click(function () {
        trackThis();
        existingHandler()
    });

What is happening with #3 solution in mine is that I m getting Uncaught TypeError: Object [object Window] has no method trigger error.

任何提示都会感激不尽

最佳回答

如果两个处理器都附在jQuery 中, 那么只需设置两个点击处理器 。 在内部, jQuery 执行“ 观察模式 ”, 这样它就可以将处理器集中起来, 并按添加的顺序按顺序执行 。

$( #btnPrint ).on( click , firstHandler);
$( #btnPrint ).on( click , secondHandler);

这些发言不必是连续的,甚至不必是同一范围的。

如果 HTML 中已经指定了处理器, 则适用同一原则。 jQuery 将仅在原始处理器之后执行任何其他处理器 。

HTML :

<input type= button  id= btnPrint  value= Print  onclick=".....;" />

jj 查询 :

$( #btnPrint ).on( click , secondHandler);

"http://jsfiddle.net/qUCJ3/1/" rel=“not follow”>iddle

EDIT ...

也有可能遵守这样的命令,即最初的处理者在JQuery所附的处理者之后执行命令:

var $btnPrint = $( #btnPrint );
var originalHandler = $btnPrint.get(0).onclick;
$btnPrint.get(0).onclick = null;

$btnPrint.on( click , secondHandler);
$btnPrint.on( click , originalHandler);

"http://jsfiddle.net/qUCJ3/2/" rel = "no follow" > fiddle

问题回答

如果我正确理解你的问题

这项工作应当奏效,

$( #btnPrint ).off("click", "**" ).on( click ,function(){
     trackThis();
     existingHandler(); 
})

i. 即先 < code>off 现有的事件处理器,然后是 on your custom one.

如果这对你没有帮助, 这可能是你想要的一个,

< 强力 > 编辑 < /强 >

如果您想要在单击 时覆盖内线 , 您可以使用这种肮脏的方法 。

<强度>j查询代码:

$(function(){ 
        var mydiv = $( #mydiv );
        var inlineClick = mydiv.attr( onclick );

        mydiv.removeAttr( onclick ).click(function(){
            // your code 
            alert( mycode );

            eval(inlineClick);

            // or first do, eval(inlineClick);
            //then your code later

        })
    });     
    function inlinefun(){
        alert( I am inline function ); 
    }

<强>马库普:

<div id= mydiv  onclick= inlinefun(); >Mydiv</div> 

没有邪恶的 Eval < / 强 > (没有邪恶的 Eval < / 强 > )

(不能在每种情况下都工作,只是给你一个指示)

$(function(){ 
        var mydiv = $( #mydiv );

            // Dirty way to get function name and its args by a string.
    var inlineClick = mydiv.attr( onclick ).split( ( );
    var funName = inlineClick[0];
            var args = inlineClick[1].replace( ) ,  );


    mydiv.removeAttr( onclick ).click(function(){
        // your code 
        alert( mycode );  

        // The default inline function
        window[funName](args);

        // you can change the order as you want

    })
});     
function inlinefun(msg){
            // I know I am passing string, so removing the quotes, you need a generalised approach here
    alert(msg.replace( " ,  )); 
}

<强>马库普:

<div id= mydiv  onclick= inlinefun("I am inline function"); >Mydiv</div> 




相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...