Home > Web Front-end > JS Tutorial > Example tutorial on binding click events using jQuery

Example tutorial on binding click events using jQuery

王林
Release: 2024-02-19 22:56:22
Original
1243 people have browsed it

Example tutorial on binding click events using jQuery

jQuery click event binding example tutorial

In web development, click events are one of the most commonly used interaction methods. Through jQuery, we can easily bind click events to page elements to achieve various interactive effects. This article will introduce how to use jQuery to bind click events and provide specific code examples.

1. Introduction of jQuery library

Before using jQuery, you first need to introduce the jQuery library into the HTML file. You can link through CDN or download the jQuery file locally, and then introduce it in the HTML file.

<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
Copy after login

2. Binding click events

Using jQuery to bind click events to page elements is very simple and can be achieved through the click() method. The following is a simple example:




  jQuery点击事件绑定示例
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>





<script>
$(document).ready(function(){
  $("#btn").click(function(){
    alert("按钮被点击了!");
  });
});
</script>


Copy after login

In the above code, when the button is clicked, a warning box pops up to display "The button was clicked!". This is a simple click event binding example.

3. Event delegation

jQuery also provides an event delegation method, which can bind click events to dynamically generated elements. This can be very useful in some situations. The following is an example of event delegation:




  jQuery事件委托示例
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>



  • 第一项
  • 第二项
<script> $(document).ready(function(){ $("#list").on("click", "li", function(){ alert($(this).text() + "被点击了!"); }); $("#add").click(function(){ $("#list").append("<li>新项</li>"); }); }); </script>
Copy after login

In the above code, when a list item is clicked, an alert box pops up to display the content of the clicked item. When the "Add New Item" button is clicked, a new item is added to the list and the new item also has a click event.

Through the above examples, I believe everyone already has a certain understanding of jQuery click event binding. In actual projects, more jQuery event binding methods can be used according to needs to achieve richer interactive effects. I hope this article is helpful to everyone, thank you for reading!

The above is the detailed content of Example tutorial on binding click events using jQuery. 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