This article mainly introduces a self-written jQuery Method for Asynchronously loading data and adding events. For more information about how to add events, see tree columns in a project a few months ago, I felt a little troublesome, so I wrote one by myself, and something went wrong when I wrote it.
At that time, the project implemented permission control through the tree column. The administrator can dynamically generate tree columns for data from the database for addition, deletion, modification, and query operations, but $ (". XX "). click (); The method does not work.
1. Previously, jq1.4.3 was used. jq1.7 can all use the live () method to implement this function.
The Code is as follows:
$ ('# P'). live ('click', function (){
// Do stuff
});
However, the live method also has events that are not supported. For example, for a toggle event, you can add a click event to it, and then simulate a trigger event.
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 triggers the toggle event only after the click event is clicked. Therefore, a simulated click event is added to the event and you do not need to click to start directly.
**/
});
2. jq1.7 and later use the on method. The first attribute is the event, the second is the selector, and the third is the execution method.
The Code is as follows:
$ (Document). on ("click", "# d1", function (){
Alert ("bbbbb ");
});