Home > Web Front-end > JS Tutorial > How to trigger parent window element event in iframe

How to trigger parent window element event in iframe

php中世界最好的语言
Release: 2018-04-26 10:31:46
Original
2658 people have browsed it

This time I will show you how iframe can trigger the parent window element event , what are the precautions for iframe to trigger the parent window element event, the following is a practical case, let’s take a look take a look.

For example, the parent window defines an event.

top:

$(dom1).bind('topEvent', function(){});
Copy after login

So how do the elements in the iframe trigger the event of the parent window dom1? Is that so?

$(dom1, parent.
document
).trigger('topEvent');
Copy after login

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 another jquery Object defined events. Unless you define it in the iframe like this:

iframe:

self.$ = parent.$;
Copy after login

So the solution is very simple:

parent.$(dom1,parent.doucment).trigger('topEvent');
Copy after login

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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Tab tab scroll navigation switching implementation (with code)

jQuery is the most concise implementation of the tab option Switch

The above is the detailed content of How to trigger parent window element event in iframe. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template