jQuery bind events for dynamically added elements

1 //静态绑定事件
2 $("result ul li").bind("click",function(){
3     $(this).css("background","yellow");
4 });
5 $("result ul li").click(function(){
6     $(this).css("background","yellow");
7 });

1: When using jQuery to bind events to elements, we often use bind or click,

2: However, this can only bind events for elements that the page has already loaded.

3: If you use AJAX to request data to dynamically add page elements, the above two methods are invalid.

--------------------------------Solution---------------- ---------------------------------------------------------

  1: on() applies to current and future elements

1 $("result").on("click","ul li", function(){
2     $(this).css("background","yellow");
3 });

  2: onclick event, when adding data dynamically, bind the onclick event to the element

1 <button onclick="document.getElementById('yoo').value = func()"></button>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936560&siteId=291194637