Let's talk about the method of getting JS first. It is much more troublesome than JQuery's method. Later, the jQuery method is compared.
The JS method will be much more troublesome than JQuery, mainly because the FF browser, the FF browser will treat your change as the most DOM element
Copy code code as follows:
<div ID = "test">
<div> </div>
<div> </div>
</div>
The native JS obtains the sub -element under the element of the ID. Can be used:
var a = docuemnt.GetelementByid ("test"). GetelementsBytagname ("DIV");
At this time a.Length = 2;
But if we change another way
var b = document.GetelementByid ("test").
At this time, B.Length is no problem in the IE browser, and it is still equal to 2, but it will be 4 in the FF browser, because FF also uses the change of the bank as an element.
Therefore, we have to deal with it. We need to traverse these elements, and delete the element type into a space and the text.
Copy code code as follows:
Function Del_ff (ELEM) {
var eLEM_CHILD = ELEM.CHILDNODES;
for (var I = 0; I <elem_child.length; i ++) {
ifm_child [i]. NodeName == "#text" &&! // s/.test (elem_child.nodevalue))
{Elem.removechild (Elem_child)
}
}
}
The above functions are calendar elements everywhere. When the element has a node type is text and the node value of the text type node is empty. Delete him.
NodeNames can get a node type of a node, // S/The regular expression of non -empty characters in JS. Add before! Then it means that it is an empty character
The test () method is used to detect whether a string matches a certain mode. The syntax is: regexpobject.test (string)
If the string string contains a text that matches the regexpobject, returns true, otherwise returns false.
Nodevalue indicates the value in this node.
Removechild is a sub -element that deletes elements.
After that, before the call, father, brother, before these attributes, call the above function to clean up the space.
Copy code code as follows:
<div ID = "test">
<div> </div>
<div> </div>
</div>
<script>
fuinction dom () {
var s = document.GetelementByid ("test");
del_ff (s); // Clean the space
var chils = s.Childnodes; // get all the sub -nodes of s
Var Par = s.parentnode; // Get S's parent node
var ns = s.nextsbiling; // Get S's next brother node
var ps = s.Previouussbiling; // get the previous brother node of S
var fc = s.fringchild; // Get the first sub -node of S
var lc = s.lastchile; // Get the last sub -node of S
}
</script>
The following introduces jQuery's father, son, brother node search method
JQuery.parent (Expr) Finding a father node can be passed into Expr for filtering, such as $ ("span"). Parent () or $ ("span").
jQuery.parents (Expr), similar to jquery.parents (expr), but find all the ancestors, not limited to the parent element
jquery.children (expr). Back to all sub -nodes, this method will only return direct children nodes without returning all descendants
jQuery.Contents () returns all the following contents, including nodes and texts. The difference between this method and children () is that the blank text will also be used as a
Jquery object returns, Children () will only return to the node
jquery.prev (), return to the previous brother node, not all brother nodes
jquery.prevall (), return to all the previous brothers nodes
jquery.next (), return to the next brother node, not all brother nodes
jquery.nextall (), return to all brothers nodes after all
jquery.siblings (), return to the siblings nodes, regardless of the front and back
jquery.find (expr), completely different from jQuery.filter (expr). jquery.filter () is selected from the initial JQuery object collection, and the return result of jquery.find () will not have content in the initial set, such as $ ("p"), find ("span") , Start with <p> elements, start looking for <span>, equivalent to $ ("P span")