標籤:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.divred {
border: solid 1px red;
}
</style>
</head>
<body>
<h1>操作元素結點</h1>
<h3>屬性
<input type="button" id="" value="testAttribute" onclick="testAttribute();" />
</h3>
<ol>
<li>(對象.屬性)元素就是一個對象,屬性就是對象的屬性--擷取不到自訂屬性的值</li>
<li>(對象.get/setAtrribute)可以操作自訂屬性--只能用來擷取恒定的值</li>
</ol>
<h3>文本
<input type="button" id="" value="testText" onclick="testText();" />
</h3>
<ol>
<li>innerHTML</li>
<li>innerText</li>
</ol>
<h3>樣式
<input type="button" id="" value="testCss" onclick="testCss();" />
</h3>
<ol>
<li>類</li>
<li>具體樣式</li>
</ol>
<h3>結構
<input type="button" id="" value="testNode" onclick="testNode();" />
</h3>
<hr />
<input type="text" id="uname" value="北京尚學堂" abc="123456" />
<div id="showDiv">北京尚學堂是一個好學校</div>
<hr />
<div id="oper" class="divred">
<input type="text" id="school" value="北京尚學堂" />
</div>
</body>
<script type="text/javascript">
function testAttribute() {
var uname = document.getElementById("uname");
//操作屬性
//uname.value = "尚學堂";
//uname.type = "button";
//alert(uname.value + "----" + uname.type);
//操作屬性
//alert(uname.getAttribute("type"));
//uname.setAttribute("type", "button");
//alert(uname.getAttribute("abc"));
//alert(uname.getAttribute("value"));
}
function testText() {
var div = document.getElementById("showDiv");
//操作文本
//div.innerHTML = "welcome to bjsxt";
//alert(div.innerHTML);
//div.innerHTML = "<h1 style=‘color:red;‘>welcome to bjsxt</h1>";
}
function testCss() {
var div = document.getElementById("showDiv");
//操作類
div.className = "divred";
//操作具體樣式
div.style.height = "200px";
div.style.width = "600px";
div.style.backgroundColor = "gray";
div.style.lineHeight = "200px";
div.style.fontSize = "40px";
div.style.textAlign = "center";
div.style.fontFamily = "楷體";
}
function testNode() {
var div = document.getElementById("oper");
var school = document.getElementById("school");
//建立節點
var n = document.createElement("input");
n.type = "button";
n.value = "bjsxt" + Math.random();
n.onclick = function() {
//alert(this.value);
this.parentNode.removeChild(this);
};
//添加節點
div.appendChild(n);
//插入節點
//div.insertBefore(n, school);
//替換節點
//div.replaceChild(n, school);
//刪除節點
//div.removeChild(school);
}
</script>
</html>
js操作html的文本,屬性,元素節點 和css