Home > Web Front-end > JS Tutorial > body text

How to deal with the inability to trigger binding events when jQuery dynamically adds elements

php中世界最好的语言
Release: 2018-04-23 15:56:34
Original
1757 people have browsed it

This time I will show you how to deal with the inability to trigger binding events when jQuery dynamically adds elements Events, and what are the precautions when dealing with the inability to trigger binding events when jQuery dynamically adds elements , the following is a practical case, let’s take a look.

I recently encountered a problem, that is, after using jquery to dynamically add elements, I found that the dynamically added elements could not trigger events. Later, I checked some information on the Internet and found that it was supposed to be handled like this:

First, let’s talk about my error code:

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="external nofollow" >
  <script src="//cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
//这里是动态添加元素
      $(".add").click(function(){
        var btn = $("<button class=&#39;newBtn btn btn-default&#39;>新按钮</button>");
        $("body").append(btn);
      })<br><br>//这里是为添加的元素添加事件
      $(".newBtn").click(function(){
        alert("这里是新添加的元素触发的事件");
      })
    })
  </script>
</head>
<body>
<button class=" add btn btn-default">添加按钮</button>
</body>
</html>
Copy after login

Here’s my solution

Method 1: Bind live events (live events are only supported below jquery 1.9, not higher versions).

$(".newBtn").live("click",function(){
///jquery 1.9(不包括1.9)以下可以
  alert('这里是动态元素添加的事件');
})
Copy after login

Method 2: Use on() event binding($(ParentEle).on("click",".thisEle",function(){ })

$("body").on("click", ".newBtn", function() {
   alert('这里是动态元素添加的事件');
});
//这里的ParentEle是 thisEle的父辈元素或者祖先元素,ParentEle可以是document,也可以是body等。
//注意:如果此时调用的函数是外部定义好的函数,那在调用的时候不要加(),不然会跳过点击事件直接触发函数
Copy after login
$("body").on("click", ".newBtn",aa );
function aa(){
    alert('这里是动态元素添加的事件');
}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

What are the precautions for jQuery version upgrade

Detailed explanation of jQuery plug-in encapsulation steps

The above is the detailed content of How to deal with the inability to trigger binding events when jQuery dynamically adds elements. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!