Method example for passing the current element by encapsulating functions in jquery. jquery example
Recently, I encountered a problem in my work. I need to perform ajax operations on a group of elements on the page. The structure is as follows:
<div id="aid"></div> <div id="aid"></div> <div id="aid"></div> <div id="aid"></div> <div id="stop">Stop here</div> <div id="aid"></div> <div id="aid"></div> <div id="aid"></div>
Write traversal Functions
function a() { $('div').each(function () { var that = $(this); var id = that.attr('id'); b(id, that) }) }
First, execute the Functiona()
Traverse each element and then executeb()
Function ajax
Function B (aId, that) {$. ajax ({url: 'ajaxhandler. ashx ', data: {aid: aid}, ype: 'text', type: 'post', async: true, success: function (data) {var content = ''; if (data = 'true') {content = "correct" ;}else {content = "error" ;}that.html (content );}})}
Assign different values to the clicked elements based on the returned values.
Because some html elements have the same style$ ("# Id" pai.html ("value assignment ");
Assign values
Use this here to assign the returned value to the element of the current click.
Because this page function is nested, you need to pass the current element,
However, for various reasons, it may be difficult to use this at the end and still obtain the correct current element. Therefore, you can use a variable to store this:
var that = $(this);
Then you can use it.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.