Javascript--DOM文档对象模型
| 节点名称 | 节点值 | 节点类型 | |
| 元素节点 | 大写的标签名 | null | 1 | 
| 文本节点 | #text | 内容本身 | 3 | 
| 属性节点 | 属性名 | 属性值 | 2 | 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="#" class="box" name="box" id="box">hello world</a>
</body>
</html>
<script>
    var aBox = document.querySelector('#box');// 获取节点对象
    // 元素节点
    console.log(aBox.nodeName); // A      // 获取节点名称(大写的标签名)
    console.log(aBox.nodeValue); // null  // 获取节点值(在这里,元素本身作为一个节点没有值)
    console.log(aBox.nodeType);  // 1     // 获取节点类型(1,2,3)
    // 文本节点
    // 在这里,firstChild--返回被选节点的第一个子节点
    console.log(aBox.firstChild.nodeName);  // #text
    console.log(aBox.firstChild.nodeValue); // hello
    console.log(aBox.firstChild.nodeType);  // 3
    // 属性节点
    // 在这里,getAttributeNode--从当前元素中通过属性名获取属性节点
    console.log(aBox.getAttributeNode('href').nodeName);  // href
    console.log(aBox.getAttributeNode('href').nodeValue); // #
    console.log(aBox.getAttributeNode('href').nodeType);  // 2
    // obj.getAttributeNode(“href”);  从当前元素中通过名称获取属性节点
</script>
element.childNodes 获取元素/节点的节点列表,包括文本节点、注释节点
element.children 获取元素/节点的节点列表,只有元素节点

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <div id="box" class="box1 box2 box3 box4 box5">
    </div>
</body>
<script>
/* 1 */    console.log(box.classList);    // 获取id为box标签的class列表
            box.classList.add("box6","box7")    // 向class列表中添加"box6","box7"两个类名
/* 2 */    console.log(box.classList);    // 再次输出,可以明确看到多了两个类名
/* 3 */    console.log(box.classList.length);    // 获取类名的长度 输出:7
/* 4 */    console.log(box.classList.contains("box3"));    // 在class列表中查询是否有"box3"这个类名,有则返回true,无则false
/* 5 */    console.log(box.classList.item(1));    // 获取class列表中下标为 1 的类名
            box.classList.remove("box6","box4")    // 删除在class列表中类名为"box6","box4"类名
/* 6 */    console.log(box.classList);    // 再次输出class列表
</script>
</html>
element.style 获取或设置元素的样式属性
element.attributes 获取指定节点属性列表
element.parentNode 获取指定元素的父节点
element.firstElementChild 获取指定元素的第一个子元素
element.firstChild 获取指定元素的第一个子节点

element.getAttribute(属性名) 通过属性名获取属性值
element.getAttributeNode(属性名) 通过属性名获取属性节点
element.lastChild 获取最后一个子节点,包含文本节点和注释节点(取决哪个在最后)
element.lastElementChild 获取最后一个子节点(忽略文本节点和注释节点)
element.previousSibling 获取指定元素的前一个兄弟元素(处在同一层)(包括文本节点、注释节点)
element.previousElementSibling 获取元素节点之前的兄弟元素节点(不包括文本节点、注释节点)
element.tagName 获取元素的标签名(大写的标签名)
element.textContent 获取仅获取元素的文本内容,设置元素即覆盖元素的全部内容
<html>
<body>
    <div id="box1">hello world</div>
    <div id="box2">
        hello world
    </div>
</body>
<script>
    console.log("111",box1.textContent);
    console.log("111",box2.textContent);
    box1.textContent = "你好呀"    // 这里将box1中的"hello world"替换为"你好呀"
    box2.textContent = "你好呀"    // 设置即覆盖掉id为box2标签,替换为指定内容
    console.log("222",box1.textContent);
</script>
</html>
element.title element.title = newTitle 获取和设置元素的标题
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>dsadasdasd</title>
</head>
<body>
    <div>...</div>
</body>
<script>
    console.log(document.title); // 输出:dsadasdasd
</script>
</html>
element.setAttribute(属性名,属性值) 创建或改变某个新属性
element.setAttributeNode(属性节点) 用于添加新的属性节点
element.removeAttribute(属性名称) 从元素中删除指定的属性element.removeAttributeNode(属性名称) 从元素中删除指定的属性节点
element.removeChild() 删除一个子元素
    // 获取一个类名为box的标签
    let eleBox = document.querySelector(".box");
    // 删除子节点,childNodes--可返回元素的节点列表,删除eleBox元素中,节点列表下标为1的节点
    eleBox.removeChild(eleBox.childNodes[1]);
element.appendChild(newNode) 向元素的子节点列表末尾添加一个新的元素/节点
element.insertBefore(newNode,oldNode) 向元素的子节点列表oldNode节点前面添加一个新的元素/节点
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>dsadasdasd</title>
</head>
<body>
    <div id="box" class="box1 box2 box3 box4 box5">
        <p>1</p>
        <p>2</p>
        <p>3</p>
    </div>
</body>
<script>
    let box = document.getElementById("box");
    let boxP = box.getElementsByTagName("p")[1]
    console.log(boxP);
    box.className = "newBox";
    console.log(box.classList);
    let newNode = document.createElement("p");
    newNode.innerText = 0
    box.insertBefore(newNode,boxP)
</script>
</html>
修改现有样式表
document.styleSheets[0].insertRule(".box{font-size:26px;line-height:50px;width:200px;height:50px}")
                    
                
                
            
        
浙公网安备 33010602011771号