使用setAttribute為什么不行
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style type="text/css">
.message{ ? ?
width:200px;
height:100px;
background-color:#CCC;}
</style>
</head>
<body>
<script type="text/javascript">
var ptag=document.createElement("p");
p.setAttribute("name","message");
var textnode=document.createTextNode("I love u");
ptag.appendChild(textnode);//變量不要加雙引號,否則無效
document.body.appendChild(ptag);
</script>?
</body>
</html>
2017-04-08
p.setAttribute("name","message");
首先p是html標簽,用p是錯誤的,要改成ptag。css中的message是類,所以你的name要改成class才行
正確應該是:ptag.setAttribute("class","message");
2017-08-06
結(jié)果都是:
2017-08-06
好像setAttibute(name,value)里的參數(shù)名name不論是英文大寫還是小寫,實際運行之后都變成了小寫。所以我用setAttribute沒法設置innerHTML(HTML為大寫)。
2017-04-04
p沒有name屬性,詳見官網(wǎng)http://www.w3school.com.cn/tags/html_ref_standardattributes.asp
2017-03-17
如果你想要實現(xiàn)CSS 代碼 ?a.className="message"; ?這樣就能顯示。
setAttribute("要設置的屬性名字","要改變的值")
這個屬性前些章節(jié)學過。 ?我沒記錯的話 好像只能修改字符串 等等。。
不能用來添加CSS 樣式。