Home >Web Front-end >JS Tutorial >jquery code for elements in iframe to trigger parent window element events_jquery
For example, the parent window defines an event.
top:
$(dom1).bind('topEvent', function(){});
So how do the elements in the iframe trigger the event of the parent window dom1? Is that so?
$(dom1, parent.document).trigger('topEvent');
It seems correct, but it is actually misleading.
Because the jquery object of the parent window and the jquery object in the iframe are actually two objects (functions), the jquery in the iframe will not trigger events defined by another jquery object. Unless you define it like this in iframe:
iframe:
self.$ = parent.$;
So the solution is simple:
parent.$(dom1,parent.doucment).trigger('topEvent');
Just call the parent jquery to execute the event.
It is said that iframe should no longer need to import jquery files. It can just share the same jquery with the parent window. This is very environmentally friendly.