拦截javascript事件

时间:2009-09-14 12:28:42

标签: javascript jquery

这是我正在尝试做的事情:

我有一个包含一些链接的页面。大多数链接都在 onclick 事件上附加了一个函数。

现在,我想将css类设置为某些链接,然后每当单击其中一个链接时,我想执行某个函数 - 在它返回后,我希望链接执行附加到它的onclick函数。

有没有办法做我想要的?我正在使用jQuery,如果它有所作为。

以下是一个例子的尝试:

$("#link").click(function1);
$("#link").click(function2);
$("#link").click(function(){
   firstFunctionToBeCalled(function (){
      // ok, now execute function1 and function2
   });
}); // somehow this needs to be the first one that is called


function firstFunctionToBeCalled(callback){
    // here some user input is expected so function1 and function2 must not get called
    callback();

}

所有这一切都是因为我被要求为许多按钮放置一些确认框(使用方框),我真的不想通过每个按钮。

4 个答案:

答案 0 :(得分:1)

如果我理解正确的话,这就是你想要做的......

var originalEvent = page.onclick;   //your actual onclick method
page.onclick = handleinLocal;       //overrides this with your locaMethod

function handleinLocal()
{     ...your code...     
   originalEvent (); 
 // invoke original handler
}

答案 1 :(得分:0)

我会使用jQuery的unbind来删除任何现有事件,然后绑定一个函数,该函数将按照我想要的顺序编排我想要的事件。

绑定和取消绑定都在jquery.com上的jQuery文档中并且像这样工作......

$(".myClass").unbind("click"); // removes all clicks - you can also pass a specific function to unbind

$(".myClass").click(function() {
    myFunctionA();
    myFunctionB($(this).html()); // example of obtaining something related to the referrer
});

答案 2 :(得分:0)

丑陋的黑客将使用mousedown或mouseup事件。这些将被称为before the click event

答案 3 :(得分:0)

如果您可以在其余处理程序之前添加事件处理程序,则可以尝试使用jQuery的stopInmediatePropagation

相关问题