IE fireEvent method overview and _ javascript tips-js tutorial

Source: Internet
Author: User
IE provides a fireEvent method, which is probably used to trigger an event. I thought it was the same as onclick, if you have time to look at the details of javascript, many netizens also provide a fireEvent method in IE. As the name suggests, it is what triggers an event. At the beginning, I thought it would be the same as using onclick () in normal times. I did not expect to find it when I was writing a javascript getting started ppt. I was so self-righteous! It seems that there are still a lot of javascript details not mastered!

Now, we will record the use of the fireEvent method in detail based on our summary. FireEvent is a method provided by IE, msdn documentation address: http://msdn.microsoft.com/en-us/library/ms536423 (v = vs.85). aspx

Onclick ()
Let's first look at the first instance code:

The Code is as follows:



  • I am one;

  • I am two;

  • I am three;


Click me!


In this Code, we do not have an id1 li add onclick event. When we click the button, an error is returned, indicating that "the object does not support this attribute or method ". It can be seen that DOM. onclick () can be used only after the onclick event is added.

If we change the above Code:

The Code is as follows:



  • I am one;

  • I am two;

  • I am three;


Click me!


Click the button to trigger the onclick event, but the ul onclick event is not triggered. This indicates that DOM. onclick () does not have bubbles.
FireEvent ()
Let's see if fireEvent and onclick () trigger events are the same. See the following code:

The Code is as follows:



  • I am one;

  • I am two;

  • I am three;


FireEvent!


After clicking the button, the ul onclick event is triggered, indicating that fireEvent will cause bubbling, and the message "the object does not support this attribute or method" is not displayed like onclick ", this indicates that the onclick event can be bubbling even if id1 is not added.
From this we can see that the fireEvent method in IE is similar to simulating users' Mouse clicking behavior, rather than simply onclick.

Summarize the differences between fireEvent and onclick
The preceding example shows that the fireEvent and onclick of DOM have the following differences:
1. The onclick event must be added to the DOM. Otherwise, the error "the object does not support this attribute or method" will be reported.
2. onclick will not cause the IE bubble process, but fireEvent will cause the bubble. fireEvent is more close to the user's real behavior trigger.
3. According to the second article, fireEvent can be fireEvent even if the DOM does not have a click event, without reporting an error (more close to the real user behavior)
Finally, you can test the code below.:

The Code is as follows:



  • I am one;

  • I am two;

  • I am three;


Click me!
FireEvent!

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.