Javascript code _ javascript tips for simulating jQuery's siblings Effect

Source: Internet
Author: User
Take the brother node of a DOM element and use the native JS Code to do it in the form of jQuery's siblings method. The Code is as follows:


Function siblings (o) {// The parameter o is the sibling node of the user who wants to retrieve the element.
Var a = []; // defines an array to store the brother elements of o.
Var p = o. previussibling;
While (p) {// The brothers who take o first determine whether there is any previous brother element. If so, execute p to indicate previussibling.
If (p. nodeType = 1 ){
A. push (p );
}
P = p. previussibling // Finally, assign the previous node to p
}
A. reverse () // reverse the order so that the order of elements is sequential.
Var n = o. nextSibling; // obtain the younger brother of o.
While (n) {// determine if there is any younger brother node. n indicates nextSibling.
If (n. nodeType = 1 ){
A. push (n );
}
N = n. nextSibling;
}
Return a // Finally, return this group of elements in the order from the boss to the old and small
}

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.