Javascript jquery Gets the sibling element of the current element/prev/Next Element (RPM) __java

Source: Internet
Author: User

var chils= s.childnodes; Get all of S's sub nodes var
par=s.parentnode//Get S's parent node var ns=s.nextsbiling;//Get S's
next sibling node
var ps= s.previoussbiling; Get the last sibling of S of var fc=s.firstchild;//Get the
first child node of s the
var lc=s.lastchile;//Get final child node of S

JS gets node parent, child element

First of all talk about the acquisition of JS, it is much more troublesome than the jquery method, followed by the jquery method for comparison.

The JS method is much more cumbersome than jquery, mainly because the FF browser, FF browser will make your line wrap also as a DOM element

<div id= "Test" >
    <div></div>
    <div></div>
</div>

Native JS Gets the child element under the element with the ID test.

Can be used:

var a = Docuemnt.getelementbyid ("Test"). Getelementsbytagname_r ("div");

This is no problem.

At this time a.length=2;

But if we change the other way,

var b =document.getelementbyidx_x ("Test"). ChildNodes;

At this point, b.length in IE browser is not a problem, it is still equal to 2, but in the FF browser will make 4, because the FF also as an element of the newline.

So, here we are going to do the processing, we need to traverse these elements, the element type is a space and the text is deleted.

function Del_ff (elem) {
    var elem_child = elem.childnodes;
    for (var i=0 i<elem_child.length;i++) {
        if elem_child[i].nodename = = "#text" &&!/\s/.test (elem_ Child.nodevalue))
        {
            elem.removechild (elem_child)
        }
    }
}    

The above function traverses the child element, when the node type inside the element is text and the node value of the text type node is empty. Just to remove him.

Nodenames can get the node type of a node,/\s/the regular expression of the non-empty character in JS. Front Plus. , the null character test () method is used to detect whether a string matches a pattern. Syntax: Regexpobject.test (String)

Returns True if string strings contain text that matches regexpobject, or false.

NodeValue represents the value in this node.

RemoveChild is the child element of the deletion element.

Then, before calling the son, father, brother, these properties, call the above function to clean up the blanks.

<div id= "Test" >
    <div></div>
    <div></div>
</div>
<script>
    function dom () {
        var s= document.getelementbyidx_x ("test");
        DEL_FF (s); Clear Space
        var chils= s.childnodes;//Get all of S's sub nodes
        var Par=s.parentnode;//Get the parent node
        var ns=s.nextsbiling of S; Get S's next sibling node Var ps=s.previoussbiling;//Get the last sibling of S's fc=s.firstchild var;//Get the
        first child node of s Var
        lc= S.lastchile; Get the last child node of S
</script>

The following is a description of the parent, child, and sibling node lookup method of jquery

jquery.parent (expr) to the Father node, you can filter through expr, such as $ ("span"). Parent () or $ ("span"). Parent (". Class ") jquery.parents (expr), similar to jquery.parents (expr), but finds all ancestor elements, not limited to the parent element Jquery.children (expr). Returns all child nodes, This method only returns a direct child node, does not return all Descendants node Jquery.contents (), and returns all of the following, including nodes and text.
The difference between this method and children () is that the inclusion of blank text is also returned as a JQuery object, and children () only returns the node Jquery.prev (), returning to the last sibling node, not all sibling nodes Jquery.prevall (), returns all previous sibling nodes Jquery.next (), returns the next sibling node, not all sibling nodes Jquery.nextall (), returns all the sibling nodes jquery.siblings (), Return to sibling node, jquery.find (expr) before and after, completely different from jquery.filter (expr). Jquery.filter () is a subset of the initial set of JQuery objects, and the return result of Jquery.find () will not have the contents of the initial collection, such as $ ("P"), Find ("span"), from the <p> element start <span>, equivalent to $ ("P span") 
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.