Today Stroll Q Group met a netizen ask questions:
The problem is that he needs to find the number of each containing sub001 in the element where the ID is the beginning of the Sch.
such as: id:sch002, sch033 div elements, and so on the following numbers are random, the inside will contain sub001 elements, the number of different; the first netizen is to want to use for the loop to solve, will undoubtedly recruit Because the first loop volume is large (because you cannot predict how small the number of sch numbers will need to be set) causing the running loss of memory, running slowly, and finding that no element operation will be error-free.
So I introduced him to use ^= jquery's wildcard selector, which really made it easy to solve his problems right away; After the original one he raised another question, what do I need to do to get the number behind Sch separately? Actually came to this step of thinking are clear, direct access to the element is the beginning of Sch, and then $ (this) attr the id attribute, and then use JS intercept character, OK!!
Here is a small example:
$ (function () {
$ ("div[id^= ' Sch ']"). each (function () {
var schsize = $ (this). Find (' #sub001 '). Size ();
var Thisid = $ (this). attr (' class ');
Alert (the number of thisid+ "with #d1 is" +schsize);
Using substring to intercept SCH numbers
Alert (thisid.substring (3))
})
})
<div id= "sch002" >
<div id= "sub001" ></div>
<div id= "sub001" ></div>
<div id= "sub001" ></div>
<div id= "sub002" ></div>
</div>
<div id= "sch033" >
<div id= "sub001" ></div>
<div id= "sub002" ></div>
<div id= "sub001" ></div>
<div id= "sub002" ></div>
</div>
To popularize:
1. Selector
(1) wildcard characters:
$ ("input[id^= ' Code ')");//id attribute all input tags starting with code
$ ("input[id$= ' Code ')");//id attribute all input tags ending with code
$ ("input[id*= ' Code ')");//id property contains all input tags for code
(2) Select by index
$ ("tbody Tr:even"); Select all TR labels indexed to even
$ ("tbody tr:odd"); Select all TR labels with an odd index
(3) Get the number of input of Jqueryobj next level node
Jqueryobj.children ("input"). Length;
(4) Get all <a> tags under sub-nodes of the tag class main
$ (". Main > a");
(5) Select next to label
Jqueryobj.next ("div");//Gets the jqueryobj tag immediately after the next div,nextall gets all
2. Filters
Not
$ ("#code input:not ([id^= ' code])");//id is a code tag that does not contain all input tags that start with the ID code
3. Events
Working with keyboard actions on a text box
Jqueryobj.keyup (function (event) {
var keycode = event.which;//Gets the key value of the currently pressed keyboard, the Enter is 13
}
4. Tool functions
$ (' #someField '). Val ($.trim ($ (' #someField '). Val ()));//Clear space, Syntax: $.trim (value)
not going to the meeting, just a process!
This article is from the "Happy and simple" blog, make sure to keep this source http://1120173856.blog.51cto.com/2882946/1637469
Jquery Wildcard Selector