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

jQuery asynchronously loads data and adds events example_jquery

WBOY
WBOYOriginal
2016-05-16 16:38:521232browse

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
});

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事件,所以就给他加上模拟点击事件,不需要点击直接出发
**/
});

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");
});
Statement:
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