强制运行自定义函数befor运行元素的已分配事件处理程序

想象一下jQuery插件向页面插入元素(例如图像),图像具有Click事件的事件处理程序。 例如,当我点击图像打开一个弹出div。

另一方面,我在我的页面中编写了一个函数。 例如,慢速淡化文本框(输入标记)的函数。

所以我们有:

  • 一个jquery插件
  • jquery插件添加的图像 – >>> id =“MyImage”
  • 点击图像事件原因— >>弹出一个div
  • 输入标签— >> id =“MyInput”
  • 我隐藏输入的函数— >> hideSlowInput()

重要说明:我不想更改插件。 我想覆盖图像的事件处理程序而不更改插件。

编辑:我没有打开popupdiv的代码,我只是在$(document).ready中启动插件

现在,每次单击图像后,如何在运行我的函数后强制打开弹出窗口?

关注@ Ejay的评论

这基本上是不可能的,至少在文档中没有公开支持{我无法找到…}

但是如果你正在谈论使用jquery绑定打开的popup div处理程序的插件,你可以试试这个:

//declare your function function myfunction(){ alert('ok'); } //bind click handler to image with class 'myclass' $('.myclass').click(myfunction); //set this handler in first with keeping other already bound events var myH = $._data( $(".myclass")[0], "events" ).click.pop(); $._data( $(".myclass")[0], "events" ).click.unshift(myH); 

如果没有其他点击处理程序,您可以按顺序颠倒:

 [].reverse.call($._data($('.myclass')[0]).events.click); 

请在此处查看通用演示

不明白你在这里需要什么(除非你提供相关的代码),但我认为你需要在这里委托点击事件..

由于图像是动态添加的,因此需要在委托事件中使用..

  $(document).on('click','#MyImage',function(){ alert('clicked image'); //codes to open your popopdiv }); 

注意:由于添加了图像,您可能最终对图像具有相同的IDS(除非您只添加一个图像),这是无效的.ID应该始终是唯一的……如果您可以添加图像,那将更好class而不是id …并使用类选择器.Myimage ..并将其委托给最接近的statis父容器比文档本身更好。

关注@焙烤的答案

我强制旧事件处理程序作为新事件处理程序的回调运行:

例:

 $('#myBtn').click(function(){ alert(1); }); $('#myBtn').click(function(){ alert(2); }); $('#myBtn').click(function(){ alert(3); }); $('#myBtn').click(function(evt){ alert(4); }); //------Adding A Custom Function Befor All Click Event Handlers And Forcing Theme Run As Callback Of The Custom Function-------- console.clear(); var objEvents=jQuery.extend(true, {}, $._data( $("#myBtn")[0], "events" )); $('#myBtn').unbind('click'); var callEventHandlers=function(myEventName,myEvents){ $.each(myEvents, function(i, event) { if(i==myEventName){ $.each(event, function(j, h) { // console.log(h.handler.toString()); h.handler.call() }); } }); } $('#myBtn').click(function(evt){ $(this).fadeOut(3000,function(){callEventHandlers('click',objEvents)}); });