<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" >
function GetDom01 () {
var div = document.queryselector (". box");
Get the Attribute Class property
Console.log (Div.classname);
The value is obtained.
Console.log (Div.getattribute ("class"));
Get Set
var attrs = div.attributes;
for (Var i=0;i<attrs.length;i++) {
var attr = attrs[0];
Console.log (Attr.value);
}
Console.log ("--------------------");
Console.log (Div.nodename);
Console.log (Div.nodetype);
Console.log (Div.nodevalue);
Console.log (DIV)
}
function SetAttr () {
var div = document.queryselector (". box");
div[' Test ' = "ttttt";
Set the value of a property
Div.setattribute ("Testtest", "tttttt");
Div.setattribute ("name", "Divdiv");
Div.name = "Divdiv";
Div.removeattribute ("name");
}
function Settexttext () {
var div = document.queryselector (". box");
Console.log (div.textcontent)
Console.log (div.innerhtml);
Change the amount of content in the text
div.innerhtml = "}
/**
*
* Element node attribute node text node
*
* NodeName element name Attribute name #text
*
* NodeType 1 2 3
*
* NodeValue Null attribute value text content
*
*/
Get all the child elements
function Getchilds (_parent) {
var childs = [];
if (_parent) {
var child = _parent.childnodes;
for (Var i=0;i<child.length;i++) {
var c = Child[i];
if (C.nodetype = = = 1) {
Childs.push (c);
}
}
}
return childs;
}
function Testgetchilds () {
var childs = Getchilds (Document.queryselector (". box"));
Console.log (Childs)
}
/**
* Generic Gets the previous next element
*/
function getsibling (_dom,sibling) {
if (_dom) {
Sibling = Sibling | | "NextSibling";
_dom = _dom[sibling];
while (_dom && _dom.nodetype! = 1) {
_dom = _dom[sibling];
}
return _dom;
}
return null;
}
Get the next element of the sibling
function getnextsibling (_dom) {
if (_dom) {
_dom = _dom.nextsibling;
while (_dom.nodetype! = 1) {
_dom = _dom.nextsibling;
}
return _dom;
}
return null;
}
function testgetsibling () {
Console.log (Getsibling (Document.queryselector (". Box"), "previoussibling"))
}
Get previous node
function getprevioussibling (_dom) {
Return getsibling (_dom, "previoussibling");
}
function Teststyle () {
var div = document.queryselector ("#box2");
Div.style.border = "1px solid red";
}
function TestStyle1 () {
var div = document.queryselector ("#box2");
Considerations for changing Styles
When it's not a regular word,
Div.style.border = "None";
Assigning values in brackets
div.style["Margin-left"] = "10px";
Meet the Hump naming
Div.style.marginRight = "20px";
}
</script>
<body>
<input type= "button" onclick= "GetDom01 ()" value= "GetDom01"/>
<input type= "button" onclick= "SetAttr ()" value= "SetAttr"/>
<input type= "button" onclick= "Settexttext ()" value= "Settexttext"/>
<input type= "button" onclick= "Testgetchilds ()" value= "Testgetchilds"/>
<input type= "button" onclick= "testgetsibling ()" value= "testgetsibling"/>
<input type= "button" onclick= "Teststyle ()" value= "Teststyle"/>
<input type= "button" onclick= "TestStyle1 ()" value= "TestStyle1"/>
<div class= "box" name= "div" >
<div id= "Box1" style= "" >
<span>this is a span in Div 1</span>
<span>this is a span in Div 2</span>
</div>
<div id= "Box2" >
<span>this is a span in Div 1</span>
<span>this is a span in Div 2</span>
</div>
<div id= "Box3" >
<span>this is a span in Div 1</span>
<span>this is a span in Div 2</span>
</div>
<div id= "Box4" >
<span>this is a span in Div 1</span>
<span>this is a span in Div 2</span>
</div>
</div>
</body>
Get parent node, sibling node and processing attribute node in JS