Home > Web Front-end > JS Tutorial > Detailed analysis of IE's fireEvent method in JavaScript_javascript skills

Detailed analysis of IE's fireEvent method in JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:14:01
Original
1131 people have browsed it

In IE, a fireEvent method is provided. As the name suggests, it means triggering an event to occur. At first, I thought it would be the same as using onclick(). Unexpectedly, I discovered it recently when I was writing an introductory JavaScript ppt. It turned out that I was too self-righteous! It seems that there are still many details of JavaScript that have not been mastered!

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

onclick()
Let’s look at the first example code first:


  • i am one;

  • i am two;

  • i am three;


< button onclick='document.getElementById("id1").onclick();'>click me!

In this code, we do not have an li with id1 to add an onclick event. When the button is clicked, an error will be reported, prompting "The object does not support this attribute or method." It can be seen that DOM.onclick() needs to add an onclick event before it can be used.

If we modify the above code to:


  • i am one;
  • i am two;

  • i am three;

  • < ;/ul>

    At this time, clicking the button will trigger the onclick event, but the onclick of ul is not triggered, which shows that there is no bubbling in DOM.onclick().

    fireEvent()
    Let’s see if fireEvent and onclick() trigger events the same. Look at the code below:


    • i am one;

    • i am two;

    • i am three;


    < button onclick='document.getElementById("id1").fireEvent("onclick")'>fireEvent !

    After clicking the button, the onclick event of ul is triggered, indicating that fireEvent will cause bubbling, and there is no prompt like onclick() "The object does not support this property or method", indicating that even if the onclick event of id1 is not added, it can bubble up. .
    It can be seen from this that the fireEvent method in IE is similar to simulating the user's mouse click behavior, rather than simply onclick.

    Summary of the difference between fireEvent and onclick
    As can be seen from the above example, DOM fireEvent and onclick (this is just a representative) have the following differences:

    Onclick requires the DOM to actually add an onclick event, otherwise it will report the error "The object does not support this attribute or method"
    Onclick will not cause IE's bubbling process, but fireEvent will cause bubbling, and fireEvent is closer to the user's reality Behavior trigger
    From the second article, fireEvent can fireEvent even if there is no click event in the DOM without reporting an error (closer to the user’s real behavior)
    Finally, you can test it with the following code:


    • i am one;
    • i am two;

    • i am three;

    • < ;/ul>

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