JQuery-----Select elements

Source: Internet
Author: User

(i) Select more elements

The Add method is used to select more elements in jquery.

The Add method supports parameters such as add (selector), add (Selector,context), add (HtmlElement), add (htmlelement[]), add (jQuery)

$ (document). Ready (function () {

var labelelems = document.getelementsbytagname ("label");
var JQ = $ ("Img[src*=daffodil]");

$ ("Img:even"). Add ("Img[src*=primula]"). Add (JQ). Add (Labelelems). CSS ("border", "thick double red");

The add parameter is selector, jquery object, and HtmlElement

});

(ii) Restrictions on the scope of selection

1) get an element from the result set

$ (document). Ready (function () {

var JQ = $ ("label");
Jq.first (). CSS ("border", "thick double Red");//First label
Jq.last (). CSS ("border", "thick double Green");//Last Label
Jq.eq (2). CSS ("border", "thick Double Black");//Second label
Jq.eq ( -2). CSS ("border", "thick Double Black");//Penultimate label

});

2) getting a subset from the result set

$ (document). Ready (function () {

var JQ = $ ("label");

Jq.slice (0, 2). CSS ("border", "thick Double Black");//index 0 and 1 label
Jq.slice (4). CSS ("border", "thick solid Red");//Label after 4

});

3) Filter Elements

The filter method can reject all elements that do not satisfy a specified condition, the supported parameters are filter (selector), filter (HtmlElement), filter (JQuery), filter (function (index))

Specify filter

$ (document). Ready (function () {
$ ("img"). Filter ("[Src*=s]"). CSS ("border", "thick double Red");//Selector
var JQ = $ ("[for*=p]");
$ ("label"). Filter (JQ). CSS ("Color", "blue");
var elem = document.getelementsbytagname ("label") [1];
$ ("label"). Filter (Elem). CSS ("Font-size", "1.5em");

$ ("img"). Filter (function (index) {
return This.getattribute ("src") = = "Peony.png" | | index = = 4;//If the function returns True, the element is preserved;

Note: src returns absolute Url,getattribute (0 returns relative URL
}). css ("border", "thick solid red")
});

Not method: its way of working is largely the opposite of filter

$ (document). Ready (function () {

$ ("img"). Not ("[Src*=s]"). CSS ("border", "thick double red");
var JQ = $ ("[for*=p]");
$ ("label"). Not (JQ). CSS ("Color", "blue");

var elem = document.getelementsbytagname ("label") [1];
$ ("label"). Not (Elem). CSS ("Font-size", "1.5em");
$ ("img"). Not (function (index) {
return This.getattribute ("src") = = "Peony.png" | | index = = 4;
}). css ("border", "thick solid red")
});

(iii) Processing result sets in a mapped manner-----only one element at a time is returned

$ (document). Ready (function () {

$ ("Div.dcell"). Map (function (index, elem) {
Return Elem.getelementsbytagname ("img") [0];//returns an IMG in each Div.dcell
}). css ("border", "thick solid red");

$ ("Div.dcell"). Map (function (index, elem) {
return $ (elem). Children () [1];//returns the second child element in each Div.dcell
}). css ("border", "thick solid blue");

});

(iv) test result set

The IS () method is used to detect whether an object in jquery satisfies the test condition, receives the parameter type with the filter, and the return value is a Boolean

$ (document). Ready (function () {

var Isresult = $ ("img"). is (function (index) {
return This.getattribute ("src") = = "Rose.png";
});
Console.log ("Result:" + isresult);

});

(v) Modify, fallback result set

1) End ()---fallback to previous result set

$ (document). Ready (function () {
Set border for the first label, set the font for all lable
$ ("label"). First (). CSS ("border", "thick solid blue")
. End (). CSS ("Font-size", "1.5em");

});

2) Addback ()---Returns the collection of the current result set and the previous result set

$ (document). Ready (function () {
$ ("Div.dcell"). Children ("img"). Addback (). CSS ("border", "thick solid Blue");//Set Dcell for all border Div and its sub-img objects
});

(vi) access to the DOM

1) accessing descendant elements

Childern Method (Children (), Children (selector)), returning child elements

The Find method (the parameter type is the same as filter, but cannot accept the function), returning the descendant element

The returned elements do not have duplicate results

2) Use the Find method to get an intersection

$ (document). Ready (function () {
var JQ = $ ("label"). Filter ("[For*=p]"). Not ("[For=peony]");
$ ("Div.drow"). Find (JQ). CSS ("border", "thick solid blue");
});

2) access to ancestor elements

Get the parent element:

$ (document). Ready (function () {
$ ("Div.dcell"). Parent (). each (function (index, elem) {//) gets each div.dcell of the parents element
Console.log ("Element:" + elem.tagname + "" + elem.id);
});

$ ("Div.dcell"). Parent ("#row1"). each (function (index, elem) {//) gets the ID of each Div.dcell that is Row1
Console.log ("Filtered Element:" + elem.tagname + "+ elem.id);
});
});

Select an ancestor element:

Parents method: All ancestor elements that can be thick (not just the parent element), can be filtered using an optional selector

$ (document). Ready (function () {
$ ("img[src*=peony], Img[src*=rose]"). Parents (). each (function (index, elem) {///Get all ancestor elements of two IMG elements
Console.log ("Element:" + elem.tagname + "" + Elem.classname + "" + elem.id);
});
});

Parentsutil (selector): Look up along the DOM tree until you match the parameter selector, note that the selection result does not include a parameter selector, and you can provide a second selector to further filter the selection results

Select the first matching ancestor element:

Closest (selector): The first ancestor element used to match a parameter selector

OffsetParent (): used to match the nearest anchor element in the ancestor element

3) access to sibling elements

Select all of the sibling elements:

Siblings (selector): sibling element for matching all parameter selectors

Select the sibling element that follows or precedes:

Prev (selector) Pervall (selector)

Next (selector) Nextall (selector)

JQuery-----Select elements

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.