Resolve the focus and blur event triggering invalid problem for the IFRAME in FF

Source: Internet
Author: User

There may be a slight difference in the conditions triggered by different browsers, and today we say that the focus and blur events of the IFRAME in the Firefox browser will trigger an invalid problem.

Solution:

1: Use Contentwindow, directly using jquery blur and focus, if you use native, add a browser to judge then Attachevent and AddEventListener on the line

The code is as follows Copy Code

$ ($ (' #frame ') [0].contentwindow). blur (function () {})
$ ($ (' #frame ') [0].contentwindow]. focus (function () {})

2:settimeout Solve It

The code is as follows Copy Code

$ (' #target '). Focusout (function () {
settimeout (function () {
$ (this). focus ();
}, 0);
});

Why is there such a problem with FF? In fact, in the HTML standard IFRAME is not these events, but IE to add. In Firefox, you can deal with this kind of event by Contentwindow this way.

Once again the depth of Google, found a foreigner wrote a very good code:

  code is as follows copy code

<script>  
Function Setit () {  
if ( document.all) {  
document.getElementById ("MyFrame"). Attachevent ("onblur", Dothis);  
}else{  
document.getElementById ("MyFrame"). Contentwindow.addeventlistener ("Blur", Dothis,false);   
}  
}  
   
Function dothis () {  
Alert ("Blurred");  
}  
</script>  
<body onload= "Setit ()" >   
<iframe  width= "height=" 144 "id=" MyFrame "></iframe>  
<input/>  

Try it really can trigger directly, the effect is the same as under IE. Back to analyze the code, found that the Firefox central Plains to the incident on the iframe hanging to the Contentwindow in the IFRAME, and the true connection in the HTML element to write event properties, IE may connect the events on the IFRAME to the Contentwindow, but Firefox is not the case, or each is different. The problem with this is that Firefox wants to give the IFRAME an extra script to add to the event.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.