DOM – 初识与ById元素
<body>
<div title=”属性节点”>测试Div</div>
</body>
<divtitle = "属性节点">测试Div</div>
<head>
<metacharset="utf-8">
<title></title>
<script>
var oDiv =document.getElementById("div1");
alert(oDiv);
</script>
</head>
<body>
<div id = "div1"></div>
</body>
<head>
<metacharset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
alert(oDiv);
}
</script>
</head>
<body>
<div id = "div1"></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
alert(oDiv.tagName);
}
</script>
</head>
<body>
<div id = "div1"></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
alert(oDiv.tagName);
}
</script>
</head>
<body>
<div id = "div1"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
}
</script>
</head>
<body>
<div id = "div1"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
alert(oDiv.id);
alert(oDiv.title);
alert(oDiv.className);
//以上是获取
document.getElementById("div1").className="aaa";
//这是修改class名
}
</script>
</head>
<body>
<div id = "div1" title="hello" class="box" style="width: 300px; height: 300px;background-color: red;"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
alert(oDiv.style.width); //获取div的宽度
}
</script>
</head>
<body>
<div id = "div1" title="hello" class="box" style="width: 300px; height: 300px;background-color: red;"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
oDiv.style.width = "666px";//修改div的宽度
alert(oDiv.style.width); //获取div的宽度
}
</script>
</head>
<body>
<div id = "div1" title="hello" class="box" style="width: 300px; height: 300px;background-color: red;"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
alert(oDiv.style.backgroundColor); //获取背景颜色
}
</script>
</head>
<body>
<div id = "div1" title="hello" class="box" style="width: 300px; height: 300px;background-color: red;"><p>早安打工人</p></div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varoDiv = document.getElementById("div1");
oDiv.innerHTML = "<h1>你好打工人</h1>"
oDiv.style.backgroundColor="yellow"; //设置背景色
alert(oDiv.style.backgroundColor);
}
</script>
</head>
<body>
<div id = "div1" title="hello" class="box" style="width:300px; height: 300px; background-color: red;"><p>早安打工人</p></div>
</body>
赞 (0)