Home > Web Front-end > Front-end Q&A > Why use jquery on

Why use jquery on

WBOY
Release: 2022-03-29 10:01:01
Original
2082 people have browsed it

In jquery, because you need to add one or more event handlers to the selected elements and sub-elements, you need to use the on() method; the event handlers added by this method are suitable for current and future Element, the syntax is "element object.on(event, event handler of child element, additional data, function)".

Why use jquery on

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

Why use jquery's on

on() method to add one or more event handlers on selected elements and sub-elements.

Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.

Note: Event handlers added using the on() method apply to current and future elements (such as new elements created by scripts).

Tip: If you need to remove the event handler, use the off() method.

Tip: If you need to add an event that only runs once and then remove it, use the one() method.

Syntax

$(selector).on(event,childSelector,data,function)
Copy after login
  • event Required. Specifies one or more events or namespaces to be added from the selected element. Multiple event values ​​separated by spaces, can also be an array. Must be a valid event.

  • childSelector Optional. Specifies that event handlers can only be added to specified child elements (and not the selector itself, such as the deprecated delegate() method).

  • data Optional. Specifies additional data to be passed to the function.

  • function Optional. Specifies a function to run when an event occurs.

The example is as follows:

<html>
<head>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").on("click",function(){
    alert("段落被点击了。");
  });
});
</script>
</head>
<body>
<p>点击这个段落。</p>
</body>
</html>
Copy after login

Output result:

Why use jquery on

After clicking the paragraph:

Why use jquery on

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of Why use jquery on. For more information, please follow other related articles on the PHP Chinese website!

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