關于className的問題
求助~,下面這個程序,為什么輸出的時候,background和width,height并沒有改變??
<style type="text/css">
.message{???
?width:200px;
?height:100px;
?background-color:#CCC;}
?
</style>
</head>
<body>
<script type="text/javascript">
document.write("輸入要創(chuàng)建的文本<br/>");
? var main1=document.getElementsByTagName("body");
? main1.appendChild(creat());
? function creat(){
??? //?? var hh=prompt("shuru");
????? var innValue=document.getElementById("inn").value;
????? var p=document.createElement("p");
????? p.innerHTML=innValue;
????? p.className="message";
????? p.style.color="red";
????? document.write(p.innerHTML);
? }
</script>
<input id="in" type="button" onclick="creat()" value="點擊創(chuàng)建文本"/>
<input id="inn" type="text" />
</body>
2016-12-16
你需要將創(chuàng)建的節(jié)點添加到當前的body下面,要不然在內(nèi)存里面,當然沒有顯示效果啦。document.body.appendChild(p);
2016-12-16
<style type="text/css">
.message{ ? ?
?width:200px;
?height:100px;
?background-color:#CCC;}
?
</style>
</head>
<body>
<script type="text/javascript">
document.write("輸入要創(chuàng)建的文本<br/>");
?var main=document.body;
?function creat(){
? var innVaule=document.getElementById("inn").value;
? var newp=document.createElement("p");
? newp.innerHTML=innVaule;
? newp.className="message";
? newp.style.color="#c00";
? main.appendChild(newp);
?}
</script>?
<input id="in" type="button" onclick="creat()" value="點擊創(chuàng)建文本"/>
<input id="inn" type="text" />
</body>