標籤:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function getDom01(){
var div = document.querySelector(".box");
//得到屬性 class屬性
console.log(div.className);
//得到的是值
console.log(div.getAttribute("class"));
//得到集合
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";
//設定屬性的值
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);
//改變文本裡面的額內容
div.innerHTML = "<h1>this is a innerHTML test</h1>";
}
/**
*
* 元素節點 屬性節點 文本節點
*
* NodeName 元素名 屬性名稱 #text
*
* NodeType 1 2 3
*
* NodeValue null 屬性值 常值內容
*
*/
//得到所有的子項目
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)
}
/**
* 通用的得到上一個下一個元素
*/
function getSibling(_dom,sibling){
if(_dom){
sibling = sibling || "nextSibling";
_dom = _dom[sibling];
while(_dom && _dom.nodeType != 1) {
_dom = _dom[sibling];
}
return _dom;
}
return null;
}
//得到同級的下一個元素
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"))
}
//擷取上一個節點
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");
//改變樣式的注意事項
//當不是一個規則單詞的時候
div.style.border = "none";
//用中括弧賦值
div.style["margin-left"] = "10px";
//滿足駝峰命名
div.style.marginRight = "20px";
}
</script>
</head>
<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"/>
<hr/>
<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>
</html>
js中擷取父節點,兄弟節點及處理屬性節點