导航
<div class="container">
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<!-- 我是注释 -->
<p>123</p>
</div>
<script>
const oContainer = document.getElementsByClassName('container')[0];
console.log(
oContainer.nodeName, // DIV
oContainer.firstChild.nodeName, // #text (第一个是文本节点)
oContainer.childNodes[3].nodeName, // #comment(第四个是注释节点)
oContainer.lastElementChild.nodeName // P(最后一个元素节点是p标签)
);
</script>
<div class="container" id="app">
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
</div>
<!-- 我是注释 -->
<p>123</p>
</ul>
<script>
const oContainer = document.getElementsByClassName('container')[0]; // 元素节点
const oBlankText = oContainer.firstChild; // 文本节点
const oComment = oContainer.childNodes[3]; // 注释节点
const oContainerIdAttr =
oContainer.getAttributeNode('id'); // container的id属性节点
const oUlClassAttr =
document.getElementsByTagName('ul')[0].getAttributeNode('class');
// ul标签的class属性节点
console.log(
oContainer.nodeValue, // null
oBlankText.nodeValue, // '\\n '
oComment.nodeValue, // ' 我是注释 '
oContainerIdAttr.nodeValue, // 'app'
oUlClassAttr.nodeValue // 'list'
)
oComment.nodeValue = '我是被修改后的注释';
console.log(oComment.nodeValue); // comment 被修改成 我是被修改后的注释
</script>
<div class="container" id="app" style="background-color: aqua;font-size: 18px;">
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<!-- 我是注释 -->
<p>123</p>
</div>
<script>
const oContainer = document.getElementsByClassName('container')[0]; // 元素节点
console.log(oContainer.hasChildNodes()); // true
</script>
<div class="container" id="app" style="background-color: aqua;"></div>
<script>
const oContainer = document.getElementsByClassName('container')[0]; // 元素节点
console.log(oContainer.attributes);
</script>
<div class="container" id="app" style="background-color: aqua;font-size: 18px;">
</div>
<script>
const oContainer = document.getElementsByClassName('container')[0]; // 元素节点
console.log(
oContainer.getAttribute('style') // background-color: aqua;font-size: 18px;
);
</script>
<div class="container" id="app" style="background-color: aqua;font-size: 18px;">
</div>
<script>
const oContainer = document.getElementsByClassName('container')[0]; // 元素节点
console.log(oContainer.getAttributeNode('style'));
</script>
function elemChildren(node) {
var arr = [],
children = node.childNodes;
for (var i = 0; i < children.length; i++) {
var childItem = children[i];
if (childItem.nodeType === 1) {
arr.push(childItem);
}
}
return arr;
}
console.log(elemChildren(div));
function elemChildren(node) {
var hash = {
'length': 0,
'push': Array.prototype.push,
'splice': Array.prototype.splice,
},
children = node.childNodes;
for (var i = 0; i < children.length; i++) {
var childItem = children[i];
if (childItem.nodeType === 1) {
// hash[hash['length']] = childItem;
// hash['length']++;
// 使用 push 的时候会 length 会自动 ++
hash.push(childItem);
}
}
return hash;
}
console.log(elemChildren(div));
document.createElement
var div = document.createElement('div');
div.innerHTML = '123';
document.body.appendChild(div);
var test = document.createTextNode('321');
document.body.appendChild(test);
var comment = document.createComment('我是注释');
document.body.appendChild(comment);
var div = document.getElementsByTagName('div')[0];
var p = document.createElement('p');
p.innerHTML = '我是p';
var text = document.createTextNode('我是text文本');
var comment = document.createComment('我是注释');
div.appendChild(p);
div.appendChild(text);
div.appendChild(comment);
<a href="">我是超链接</a>
<script>
var a = document.getElementsByTagName('a')[0];
var div = document.createElement('div');
div.innerHTML = '<p>我是段落标签</p>'
document.body.appendChild(div);
div.appendChild(a); // 会把dom中原来的a标签剪切到div内部去
</script>
<div>
<p>我是p标签</p>
</div>
<script>
var div = document.getElementsByTagName('div')[0];
var p = document.getElementsByTagName('p')[0];
var a = document.createElement('a');
a.innerHTML = '我是a标签';
div.insertBefore(a, p);
</script>
<div>
<p>我是p标签</p>
</div>
<script>
var div = document.getElementsByTagName('div')[0];
var p = document.getElementsByTagName('p')[0];
div.removeChild(p);
</script>
DOM 对象 = 元素节点
节点:
元素节点、文本节点、属性节点、注释节点、document、DocumentFragment
DOM对象
var div = document.getElementsByTagName('div')[0];
-> 元素 -> 元素节点
nodeName nodeValue nodeType attributes hasChildNodes
元素 -> 构造函数实例化 -> div节点
div new HTMLDivElement() removeChild(div)
-> div DOM对象 删除了节点,但DOM对象还存在内存
存储在内存中
<p>我是p元素</p>
<script>
var p = document.getElementsByTagName('p')[0];
p.remove();
document.body.appendChild(p);
</script>
div.innerHTML = 123;
// 追加
div.innerHTML += '111'
div.innerHTML = '<div>hhh</div>'
<div></div>
<script>
var div = document.getElementsByTagName('div')[0];
div.innerText = '<a href="" style="color: red">我是a标签</a>'
</script>
<div>
<p>我是p节点</p>
</div>
<script>
var div = document.getElementsByTagName('div')[0];
var p = document.getElementsByTagName('p')[0];
var h1 = document.createElement('h1');
h1.innerText = '我是h1标签';
div.replaceChild(h1, p);
</script>
<div class="container wrap" id="list">123</div>
<script>
var div = document.getElementsByTagName('div')[0];
console.log(div.getAttribute('class')); // "container wrap"
div.setAttribute('class', 'contain');
console.log(div.getAttribute('class')); // "contain"
</script>
<p data-age='123' data-sex='男' data-name='张三'>p标签</p>
<script>
var p = document.getElementsByTagName('p')[0];
console.log(p.dataset);
console.log(p.dataset.age);
console.log(p.dataset.name);
</script>
p.getAttribute('data-age')
p.getAttribute('data-name')