Home > Web Front-end > JS Tutorial > Overview and application of IE's fireEvent method_javascript skills

Overview and application of IE's fireEvent method_javascript skills

WBOY
Release: 2016-05-16 17:41:32
Original
1216 people have browsed it

There is a fireEvent method provided in IE, which as the name suggests 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:

Copy the code The code is as follows:


  • i am one;

  • i am two;

  • i am three;


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

In this code, we do not add onclick to the li of id1 Event, when you click the button, an error will be reported, prompting "The object does not support this property 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 :
Copy the code The code is as follows:


  • i am one;

  • i am two;

  • i am three;



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:
Copy the code The code is as follows:


  • i am one;

  • i am two;

  • i am three;




After clicking the button, the onclick event of ul is triggered, indicating that fireEvent will cause bubbling, and no onclick event occurs () prompts "The object does not support this property or method", indicating that the onclick event of id1 can bubble up even without adding it.
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, the DOM fireEvent and onclick (this is just a representative) have the following differences:
1.onclick requires The onclick event is truly added to the DOM, otherwise it will report the error "The object does not support this attribute or method"
2. onclick will not cause IE's bubbling process, but fireEvent will cause bubbling, and fireEvent is closer to the user's real behavior trigger
3. 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 :
Copy code The code is as follows:


  • i am one;


  • i am three;




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