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. 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?
Thanks
Just namespace the events.
Following is an example using 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.