Home >Web Front-end >JS Tutorial >JS method to get the brothers, parents, and child elements of a node_javascript skills
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
var a = docuemnt.getElementById("test").getElementsByTagName("div"); This is no problem
At this time a.length=2;
But if we use another method
var b =document.getElementById("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.
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, before calling the properties of son, father, brother, etc., just call the above function to clear the spaces
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, and the return result of jQuery.find() will not contain the contents of the initial collection, such as $("p"), find("span") , starting from the
element to find , which is equivalent to $("p span")