Home  >  Article  >  Web Front-end  >  jquery code for elements in iframe to trigger parent window element events_jquery

jquery code for elements in iframe to trigger parent window element events_jquery

WBOY
WBOYOriginal
2016-05-16 16:33:251169browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn