Home >Web Front-end >JS Tutorial >Self-written jQuery asynchronously loads data and adds events_jquery

Self-written jQuery asynchronously loads data and adds events_jquery

WBOY
WBOYOriginal
2016-05-16 16:48:081065browse

A few months ago, I was involved in a project involving a tree bar, and after looking at a lot of plug-ins, I found it a bit troublesome, so I wrote one myself, and every time I kept going, something went wrong.

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

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

Copy code The code is as follows:

$('#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 create a simulated click trigger event. ok
Copy code The code is as follows:

$('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');
/**
The previously bound click event will only trigger the toggle event when it is clicked, so I added a simulated click event to it and started directly without clicking
**/
});

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
Copy code The code is as follows:

$(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