Home  >  Article  >  Web Front-end  >  js jquery gets the sibling level of the current element previous next element_jquery

js jquery gets the sibling level of the current element previous next element_jquery

WBOY
WBOYOriginal
2016-05-16 15:41:191365browse

var chils= s.childNodes; //Get all child nodes of s

var par=s.parentNode; //Get the parent node of s

var ns=s.nextSbiling; //Get the next sibling node of s

var ps=s.previousSbiling; //Get the previous sibling node of s

var fc=s.firstChild; //Get the first child node of s

var lc=s.lastChile; //Get the last child node of s

JS gets node parent, child element

Let’s first talk about the JS acquisition method, which is much more troublesome than the JQUERY method. The JQUERY method will be used for comparison later.

The JS method is much more troublesome than JQUERY, mainly because FF browser will treat your line breaks as DOM elements

<div id="test">
<div></div>
<div></div>
</div>

Native JS gets the child elements under the element with 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 use another method

var b =document.getElementByIdx_x("test").childNodes; 

At this time, b.length is no problem in IE browser, it is still equal to 2, but in FF browser, it will be 4, because FF also treats line breaks as an element.

So, here, we are going to do processing. We need to traverse these elements and delete all elements that are spaces and text.

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 elements. When there is a node type in the element that is text and the node value of the text type node is empty. Just delete him.

nodeNames can get the node type of a node, /s/ is the regular expression of non-empty characters in JS. Add in front! , it means the empty character

The test() method is used to check whether a string matches a certain pattern. The syntax is: RegExpObject.test(string)

Returns true if the string string contains text matching RegExpObject, otherwise returns false.

nodeValue means getting the value in this node.

removeChild deletes the child elements of the element.

After that, before calling the properties of son, father, brother, just call the above function to clear the spaces

<div id="test">
<div></div>
<div></div>
</div>

<script>
function dom() {
var s= document.getElementByIdx_x("test");
del_ff(s);  //清理空格
var chils= s.childNodes; //得到s的全部子节点
var par=s.parentNode;  //得到s的父节点
var ns=s.nextSbiling;  //获得s的下一个兄弟节点
var ps=s.previousSbiling; //得到s的上一个兄弟节点
var fc=s.firstChild;  //获得s的第一个子节点
var lc=s.lastChile;  //获得s的最后一个子节点
}
</script>

The following introduces JQUERY’s method of finding parent, child, and sibling nodes

jQuery.parent(expr) To find the parent node, you can pass in expr for filtering, such as $("span").parent() or $("span").parent(".class")

jQuery.parents(expr), similar to jQuery.parents(expr), but searches all ancestor elements, not limited to parent elements

jQuery.children(expr). Returns all child nodes. This method will only return direct child nodes and will not return all descendant nodes

jQuery.contents(), returns all content below, including nodes and text. The difference between this method and children() is that including blank text will also be treated as a

jQuery object is returned, children() will only return nodes

jQuery.prev(), returns the previous 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 subsequent sibling nodes

jQuery.siblings(), returns sibling nodes, regardless of front or rear

jQuery.find(expr) is completely different from jQuery.filter(expr). jQuery.filter() filters out a part of the initial collection of jQuery objects, while jQuery.find()
The return result will not contain the content in the initial collection. For example, $("p"), find("span") starts from the e388a4556c0f65e1904146cc1a846bee element to find 45a2772a6b6107b401db3c9b82c049c2, which is equivalent to $("p span" )

The above introduction is about js jquery getting the sibling level of the current element. Previous Next element. I hope it will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn