js操作html的文本,屬性,元素節點 和css

來源:互聯網
上載者:User

標籤:

<!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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.