This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12901|Reply: 0

[Jquery] Obtaining elements based on attributes or attribute values in JQuery (6 ways to get them)

[Copy link]
Posted on 10/28/2015 7:30:55 PM | | |
Obtain elements based on attributes

1. For example, you want to get an element with id in the page p tag

$("p[id]").css("color","red");

Obtain elements based on attribute values
1.$。 In jQuery, $("<span>"), this syntax is equivalent to $(document.createElement("span")), which is a usage that is used when selecting an element: [attribute$=value], matching a given attribute is an element ending in some value. Here is an example:
HTML code
Copy code The code is as follows:

<input name="newsletter" />
<input name="milkman" />
<input name="jobletter" />

jQuery code:

  1. $("input[name$='letter']")
Copy code


Outcome:
[ <input name="newsletter" />, <input name="jobletter" /> ]

2.!。 Selector: [attribute!=value], matches all elements that do not contain a specified attribute, or whose attribute does not equal a specific value, this selector is equivalent to :not([attr=value]).
Here are some examples:

<input type="checkbox" name="newsletter" value="Hot Fuzz" />
<input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />

jQuery code:

  1. $("input[name!='newsletter']").attr("checked", true);
Copy code


Outcome:
[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]

3.*。 Selector: [attribute*=value], matching a given attribute is an element containing certain values. Here's an example:
HTML code:


<input name="man-news" />
<input name="milkman" />
<input name="letterman2" />
<input name="newmilk" />

jQuery code:


  1. $("input[name*='man']")
Copy code


Outcome:
[ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ]

4.@。 Match elements that contain a given attribute. Note that in jQuery 1.3, the leading @ symbol has been abolished! If you want to be compatible with the latest version, you just need to simply remove the @ symbol
Yes.

5.^。 Selector: [attribute^=value], matching a given attribute is an element that starts with some value, here is an example:
HTML code:

<input name="newsletter" />
<input name="milkman" />
<input name="newsboy" />

jQuery code:

  1. $("input[name^='news']")
Copy code


Outcome:
[ <input name="newsletter" />, <input name="newsboy" /> ]

6 Obtain an element with a specified attribute and a specified string in the setting
HTML code:

  1. <p><input type="checkbox" name="newsletter" value="Hot Fuzz"/> </p><p><input type="checkbox" name="newsletter" value="Cold Fusion" /> </p><p><input type="checkbox" name="accept" value="Evil Plans" /> </p>
Copy code


jQuery code:

  1. $("input[name$='letter'][value$='zz']").attr("checked","true");支持多条件操作
Copy code


Of course, it can also be obtained based on id attributes or other attributes, such as $("input[id=id1]").css("color",red);
In jquery, when using $("input[name='metaId']").val() cannot directly obtain the value of the selected radio, but only the first value of the radio tag, this may be related to jquery using the xpath language for lookup, and we usually want to get the value of the selected radio, there are several methods:
1. Use $("input[name='metaId']:checked").val() to get //name to represent the name attribute name in radio
2. Use $(":radio:checked").val() to get // Restrict the page to only one set of radio tags





Previous:Send a client MD5 encrypted js script
Next:Can Ruijie under Liunx log in to the campus network, who has the Ruijie client under Liunx?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com