Skip to content

Commit 104a12f

Browse files
author
朱安邦
committed
DOM
1 parent 4589d13 commit 104a12f

3 files changed

Lines changed: 75 additions & 11 deletions

File tree

DOM操作和封装DOM库/DOM面向对象.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,74 @@ Element的类型,nodeType的值为1、nodeName为元素的标签名,nodeValu
594594

595595
这个属性要注意的是,如果想获取到属性,必须先取得这个属性所在的元素;然后再进行相关的Attribute操作;这个属性可以获得自定义属性,根据HTML5规范,自定义的特性应该加上data-前缀进行验证;
596596

597+
转给getAttribute的特性名,与实际的特性名相同,因此要想得到class特性值,应该传入class而不是calssName;
597598

598599

600+
但是有两类特殊的特性,虽然有对应的属性名,但是属性的值与通过getAttribute返回的不同;
599601

602+
- 1、style
603+
- 2、onclick
600604

605+
<!doctype html>
606+
<htm>
607+
<head>
608+
<meta charset="UTF-8">
609+
<title>Document</title>
610+
</head>
611+
<style>
612+
#test{
613+
padding: 50px;
614+
background-color: red;
615+
}
616+
</style>
617+
<body>
618+
<div style="color: #999;" id="test">这是一段测试文字;</div>
619+
<script>
620+
var test=document.getElementById("test");
621+
test.onclick=function(){
622+
console.log("test被点击了")
623+
};
624+
console.log(test.getAttribute("style"));//只能获取到行内的样式;
625+
console.log(test.getAttribute("onclick"));//null
626+
627+
</script>
628+
</body>
629+
</htm>
630+
631+
通过JS操作DOM的时候,一般不适应getAttribute,而是只使用对象的属性,在取得自定义特性质的情况下,才会使用getAttribute的方法;
601632

602633

634+
**setAttribute**
635+
636+
接受两个参数,第一个参数是特性名,第二个参数是特性值;如果特性已经存在,setAttribute会以指定的值替换现有的值;如果没有的值会创建并且新增;
637+
638+
通过这个方法设置的特性名会被统一转换为小写的形式;"ID"会最终变成id,"CLASS"会被变为"class";
639+
640+
var test=document.getElementById("test");
641+
test.setAttribute("CLASS","dddddd");//class="dddddd"
642+
643+
644+
**removeAttribute**
645+
646+
彻底删除元素的特性,调用这个方法不仅清除特性的值,而且也会从元素中完全删除特性;(IE6不支持)
647+
648+
### 创建元素,
649+
650+
document.creatElement()方法,可以创建新元素,接受一个参数,就是要创建的元素,参数可以是标签名,也可以是全部的标签;
651+
652+
- div
653+
- <div style="color: #999;" id="test">这是一段测试文字;</div>
654+
655+
上面这两种都可以做为参数的;一般都使用标签名;
656+
657+
创建的元素属于游离状态,还没有被添加到文档树中,因为新建的元素并不会马上看到,可以使用appendChild,inserBefore,replaceChild()方法,
658+
659+
var test=document.getElementById("test");
660+
var creatEle=document.createElement('p');
661+
creatEle.id="test-id";
662+
creatEle.className="class-test";
663+
creatEle.innerHTML="innerHTML";
664+
test.appendChild(creatEle);
603665

604666

605667

project.lnk

697 Bytes
Binary file not shown.

test-file.html

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
<meta charset="UTF-8">
55
<title>Document</title>
66
</head>
7+
<style>
8+
#test{
9+
padding: 50px;
10+
background-color: red;
11+
}
12+
</style>
713
<body>
8-
<div id="test" title="鼠标放上来的提示" class="test-class-name" lang="en" dir="rtl"diy-data="hehe">
9-
这是一个ID为test的div内容!!!ddddd.
10-
<p>这是一段文字</p>
11-
</div>
14+
<div style="color: #999;" id="test">这是一段测试文字;</div>
15+
1216
<script>
1317
var test=document.getElementById("test");
14-
console.log(test.getAttribute("id"));//test
15-
console.log(test.getAttribute("className"));//null
16-
console.log(test.getAttribute("class"));//test-class-name
17-
console.log(test.getAttribute("title"));//鼠标放上来的提示
18-
console.log(test.getAttribute("lang"));//en
19-
console.log(test.getAttribute("dir"));//rtl(最后一个字符不能是符号,否则符号在前面显示)
20-
console.log(test.getAttribute("diy-data"));//hehe
18+
var creatEle=document.createElement('p');
19+
creatEle.id="test-id";
20+
creatEle.className="class-test";
21+
creatEle.innerHTML="innerHTML";
22+
test.appendChild(creatEle);
2123

2224
</script>
2325
</body>

0 commit comments

Comments
 (0)