Method example for passing the current element by encapsulating functions in jquery. jquery example

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.