简体   繁体   English

如何在JavaScript中的iframe对象上为事件增加唯一的自定义名称?

[英]How to gaunruntee unique custom names for events on iframe object in javascript?

I want to set jquery events on an iframe object, and then trigger them with the jquery trigger command, and these are for custom events. 我想在iframe对象上设置jquery事件,然后使用jquery trigger命令触发它们,这些事件用于自定义事件。 How can I gauruntee any custom event name I pick won't be the same as any out of the box event names that dom elements already listen to? 我如何gauruntee我选择的任何自定义事件名称都不会与dom元素已经在听的即用型事件名称相同?

Thanks 谢谢

Just namespace the events. 只是对事件进行命名空间。

Following is an example using click . 以下是使用click的示例。 Triggering the namespaced version programmatically doesn't trigger the standard one but manually clicking on the original element still triggers both event handlers 以编程方式触发命名空间版本不会触发标准版本,但是手动单击原始元素仍会触发两个事件处理程序

 // pre-exisiting click event listener $('#existing-elem').on('click', function(){ console.log('Existing event handler triggered') }); // new one you add $('#existing-elem').on('click.yourNameSpace', function(){ console.log('yourNameSpace event handler triggered') }); // listener for demo "Trigger" button $('#demo-test').click(function(){ // won't cause original non-namespaced listener to trigger $('#existing-elem').trigger('click.yourNameSpace') }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="existing-elem"> Existing element - click me </button> <button id="demo-test"> Trigger namespaced event </button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM