Home Web Front-end JS Tutorial A brief discussion on js change, propertychange, input events_javascript skills

A brief discussion on js change, propertychange, input events_javascript skills

May 16, 2016 pm 05:58 PM
change input

https://github.com/mootools/mootools-core/issues/2170

This issue comes from the implementation of checkbox and radio change events in IE (LTE8). Tested in LTE8), when you click on a checkbox or radio, its change event will not be triggered immediately unless you make it lose focus. However, in other standard browsers (including IE9), the change event is triggered immediately after clicking. Yes, this is indeed a painful problem. When it comes to the solution, it is relatively easy. Use the element-specific propertychange event in IE (LTE8) to handle it (IE9 no longer has this thing), you can avoid the above problems, such as :

Copy code The code is as follows:

checkEl.attachEvent('onpropertychange', function() {
console.log('hey man, I am changed');
});

But it is not sufficient to think that it is solved, because like Operations such as checkEl.setAttribute('data-value', 'god') will also trigger its propertychange event, so we need to use its event.propertyName to judge, such as:

Copy code The code is as follows:

checkEl.attachEvent('onpropertychange', function() {
if(window.event. propertyName == 'checked')
console.log('blah blah blah...');
});

This is OK. From this, I tested the select again, and the performance of its change event was consistent in different browsers, and there was no need to lose focus first. I tested input[type="text"] again. Its change event is familiar to us. It will only be triggered when it loses focus. So when we want it to trigger immediately as soon as something is entered, refer to the previous example. In IE (LTE8), we can use propertychange to achieve this, but the condition of propertyName becomes 'value'. In other standard browsers (including IE9), we can use a standard event input in HTML5, such as:

Copy code The code is as follows:

inputEl.addEventListener('input', function(event) {
console.log('input event fired');
}, false);

In this way, every input we make will trigger this event. Some people may ask why not use keyup to monitor it. Here is an article (original link) that explains this issue. If you are interested, you can Read.
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement laravel input hidden field How to implement laravel input hidden field Dec 12, 2022 am 10:07 AM

How to implement laravel input hidden field

How to encapsulate input components and unified form data in vue3 How to encapsulate input components and unified form data in vue3 May 12, 2023 pm 03:58 PM

How to encapsulate input components and unified form data in vue3

Detailed explanation of input box binding events in Vue documents Detailed explanation of input box binding events in Vue documents Jun 21, 2023 am 08:12 AM

Detailed explanation of input box binding events in Vue documents

What to do if there is no cursor when clicking on the input box What to do if there is no cursor when clicking on the input box Nov 24, 2023 am 09:44 AM

What to do if there is no cursor when clicking on the input box

How to use the input box carriage return event and verification function in the Vue document How to use the input box carriage return event and verification function in the Vue document Jun 20, 2023 am 09:13 AM

How to use the input box carriage return event and verification function in the Vue document

What is the input element in jquery? What is the input element in jquery? Jun 06, 2023 pm 02:18 PM

What is the input element in jquery?

How to prohibit input in html5 input How to prohibit input in html5 input Feb 03, 2023 am 10:02 AM

How to prohibit input in html5 input

Razer DeathAdder V3 HyperSpeed: New mouse offers high resolution and up to 8,000 Hz wireless polling rate Razer DeathAdder V3 HyperSpeed: New mouse offers high resolution and up to 8,000 Hz wireless polling rate Jun 22, 2024 am 12:20 AM

Razer DeathAdder V3 HyperSpeed: New mouse offers high resolution and up to 8,000 Hz wireless polling rate

See all articles