How does jQuery obtain all values of the same class label (which cannot be obtained by default )?

Source: Internet
Author: User

How does jQuery obtain all values of the same class label (which cannot be obtained by default )?

This problem happens during development because jQuery always returns only the value of the first class label, so it cannot meet our requirements.

For example:
Copy codeThe Code is as follows:
Var btn = jQuery ('. btn'). val ();

Only the value of the html element whose first class label is btn is obtained.

Obtain the values of all html elements of a group of class tags. You have to use jQuery's each traversal.
Copy codeThe Code is as follows:
Var btns = new Array (); // You can also specify var btns = [];

JQuery ('. btn'). each (function (key, value ){

Btns [key] = $ (this). val ();

// Or you can write it like this:

// Btns [key] = $ (value). val ();
});


How does jquery obtain the ID value of a sub-tag?

There is a css selector in jquery.
The code is roughly as follows:
$ (". Top1div"). each (function (){
$ (This). children (). each (
Alert ($ (this). attr ("id "));
);
});
It should not be wrong, but I did not test it.

How does jquery obtain any of multiple tags?

Any one of multiple tags?
You can choose from the jQuery selector,
$ ('# Xid') is used to query the page elements with the specified ID as "XID ".
$ ('. Cls') is used to query the page elements whose specified style class is "cls ".
$ ('Table tr td ') is used to search for table-> tr-> td by level, and returns all td
$ ('Table tr td: nth-child (2) ') returns the content of 2nd columns in the table
......
Note that jQuery looks for a set and may have multiple page elements. Use the get function to retrieve one of them.
For example, use $ ('table tr td '). get (1) to retrieve the second td.
 

Related Article

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.