Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of javascript event binding

Shulou Source: shulou.com Published: 2022-06-03 14:55:25 11月15日 Update

This article mainly introduces the methods of javascript event binding, the article is very detailed, has a certain reference value, interested friends must read it!

1. Bind with the statement "object .on event name = function () {Code}".

OBtn.onclick = function () {/ / oBtn is the event source object whose click event is bound to an anonymous function definition alert ('hi')}

2. Use the "event source .addEventListener (event name, event handler name, capture or not);" statement to bind.

Document.addEventListener ('click',fn1,false); / / click event binding fn1 function to achieve event bubbling document.addEventListener (' click',fn2,true); / / click event binding fn2 function to achieve event capture

3. Use the "onclick" attribute in the HTML tag to bind the event.

It is important to note that when you use the event attribute of the HTML tag to bind the event handler, the script code in the event attribute cannot contain a function declaration, but can be a function call or a series of script code separated by semicolons.

The above is all the content of the article "what are the methods of javascript event binding?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Events functions code properties methods content objects tags articles scripts statements processes values interests semicolons names guys buddies ways more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info vpn OPPO Reno Linux