DOM - attribute
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oDiv = document.getElementById("div1");
//访问
alert(oDiv.title);
alert(oDiv.className);
//修改
oDiv.title="xxx";
oDiv.className="box666"
alert(oDiv.title);
alert(oDiv.className);
//以上的访问和修改是我们之前学的
}
</script>
</head>
<body>
<div id="div1" title="hello" name="world" class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oDiv = document.getElementById("div1");
//我们可以通过下面的方法来进行访问和修改
//访问
alert(oDiv.getAttribute("id"));
alert(oDiv.getAttribute("title"));
alert(oDiv.getAttribute("name"));
alert(oDiv.getAttribute("class"));
//修改
oDiv.setAttribute("id","div666");
oDiv.setAttribute("title","xxx666");
oDiv.setAttribute("name","world666");
oDiv.setAttribute("class","box666");
//查看修改后的
alert(oDiv.getAttribute("id"));
alert(oDiv.getAttribute("title"));
alert(oDiv.getAttribute("name"));
alert(oDiv.getAttribute("class"));
}
</script>
</head>
<body>
<div id="div1" title="hello" name="world" class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oDiv = document.getElementById("div1");
alert(oDiv.bbb);
alert(oDiv.getAttribute("bbb"));
}
</script>
</head>
<body>
<div id="div1" title="hello" name="world" class="box" bbb="aaa" ></div>
</body>
</html>
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <script> window.onload = function(){ var oDiv = document.getElementById("div1"); oDiv.removeAttribte( “title” ); //删除该属性 }</script> </head> <body> <div id="div1" title="hello" name="world" class="box"></div> </body></html>
赞 (0)