Jschange, propertychange, and input event discussion _ javascript skills

Source: Internet
Author: User
Tags mootools
The discussion on an issue of mootools on github is very interesting, so I want to test it. Interested can click the original page to see https://github.com/mootools/mootools-core/issues/2170

This problem comes from the implementation problem of checkbox and radio change events in IE (LTE8). Test it in IE (LTE8). When you click a checkbox or radio, the change event is not triggered immediately, unless you make it lose focus, and in other standard browsers (including IE9), the change event is triggered immediately after the click, this is indeed a problem. When it comes to the solution, it is easier to handle it with the propertychange event unique to the element in IE (LTE8) (IE9 is no longer a problem ), the above problems can be avoided, such:

The Code is as follows:


CheckEl. attachEvent ('onpropertychang', function (){
Console. log ('Hey man, I am changed ');
});


However, this solution is not sufficient because it is similar to checkEl. operations such as setAttribute ('data-value', 'God') will also trigger the propertychange event, so we need to use its event. propertyName to determine, such:

The Code is as follows:


CheckEl. attachEvent ('onpropertychang', function (){
If (window. event. propertyName = 'checked ')
Console. log ('H H blah ...');
});


This is acceptable. As a result, I tested the select statement again. The change event is consistent in different browsers, and there is no need to lose focus first. I tested the input [type = "text"] again. The change event is familiar to us and will be triggered only when the focus is lost, so when we want it to be triggered immediately as soon as we input something, refer to the previous example in IE (LTE8), we can use propertychange to implement it, only the propertyName condition becomes 'value. In other standard browsers (including IE9), we can use a standard event input in HTML5, such:

The Code is as follows:


InputEl. addEventListener ('inputel, function (event ){
Console. log ('input event fired ');
}, False );


In this way, every input will trigger this event. Some people will say why keyup is not used for listening. Here is an article (the original article link) that explains this issue, you can also read it if you are interested.
Related Article

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.