Home > Web Front-end > JS Tutorial > jQuery asynchronously loads data and adds events example_jquery

jQuery asynchronously loads data and adds events example_jquery

WBOY
Release: 2016-05-16 16:38:52
Original
1276 people have browsed it

A few months ago, I was involved in a tree bar in a project, and after looking at a lot of plug-ins, I found it a bit troublesome, so I wrote one myself, and problems started to arise every time I kept doing it.

At that time, the project was controlled through a tree bar. The administrator could dynamically generate a tree bar for the data from the database to add, delete, modify and check. However, using the $(".XX").click(); method That's not possible.

1. I used jq1.4.3 before, and you can use the live() method in jq1.7 to achieve this function

$(‘#div').live(‘click',function(){
//do stuff
});
Copy after login

However, the live method also has unsupported events, such as: toggle event. When encountering this situation, you can add a click event to it, and then simulate a click trigger event and it will be ok

$('a').live('click',function(){
$(this).toggle(function(){

   alert("q11");
//
   alert($(this).attr("id"));
   $(this).parent().children('ul').show();
},function(){
  $(this).parent().children('ul').hide();
});
$(this).trigger('click');
/**
之前绑定的click事件,只有点击了才会触发toggle事件,所以就给他加上模拟点击事件,不需要点击直接出发
**/
});
Copy after login

2. For jq1.7 and above, use the on method. The first attribute is the event, the second is the selector, and the third is the execution method

$(document).on("click","#d1",function(){
alert("bbbbb");
});
Copy after login
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