<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
set/getAttribute()
removeAttrite()
【注】都是操作当前元素节点中某个属性的
*/
window.onload = function(){
var oDiv = document.getElementById("div1")
alert(oDiv.title) //hello
alert(oDiv.className) //box
alert(oDiv.getAttribute("title")) //hello
oDiv.title = "xxx"
oDiv.className = "box3"
oDiv.setAttribute("title", "xxx") //xxx
/*
上述两种方式的区别
<1>class属性范围区别,点操作是通过className,而set/getAttribute是通过class
<2>set/getAttribute是支持用户自定义属性的
<3>removeAttribte 删除属性
*/
alert(oDiv.getAttribute("class")) //box
alert(oDiv.className) //box
//设置用户自定义属性
oDiv.xxx = "yyy" //访问不到
oDiv.setAttribute("xxx", "yyy") //xxx = yyy
alert(oDiv.xxx) //undefined
alert(oDiv.getAttribute("xxx")) //yyy
oDiv.removeAttribute("title") //直接删除
oDiv.title = "" //只能赋空值 属性还在
}
</script>
</head>
<body>
<div id="div1" title="hello" name="world" class="box" xxx = yyy></div>
</body>
</html>
63 attribute方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...