我的代碼有什么問題嘛,看了半天都沒研究出來
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className屬性</title>
<style>
??? body{ font-size:16px;}
??? .one{
?? ??? ?border:1px solid #eee;
?? ??? ?width:230px;
?? ??? ?height:50px;
?? ??? ?background:#ccc;
?? ??? ?color:red;
??? }
?? ?.two{
?? ??? ?border:1px solid #ccc;
?? ??? ?width:230px;
?? ??? ?height:50px;
?? ??? ?background:#9CF;
?? ??? ?color:blue;
?? ?}
?? ?</style>
</head>
<body>
??? <p id="p1" > JavaScript使網頁顯示動態(tài)效果并實現與用戶交互功能。</p>
??? <input type="button" value="添加樣式" onclick="add()"/>
?? ?<p id="p2" class="one">JavaScript使網頁顯示動態(tài)效果并實現與用戶交互功能。</p>
??? <input type="button" value="更改外觀" onclick="modify()"/>
?? ?<script type="text/javascript">
?? ??? function add(){
?? ?????? var p1 = document.getElementById("p1");
?? ?????? document.write(p1.className+"<br>");
?? ??? }
?? ??? function modify(){
?? ?????? var p2 = document.getElementById("p2");
?? ?????? p2.className="two";
?? ??? }
?? ?</script>
</body>
</html>
感覺我敲的對著啊,為什么出不來東西。。
2016-05-15
?function add(){
?? ?????? var p1 = document.getElementById("p1");
?? ?????? document.write(p1.className+"<br>");
?? ??? }
改成
?function add(){
?? ?????? var p1 = document.getElementById("p1");
? ? ? ? ? p1.className = "one";
? ? }
2016-05-19
因為<p id="p1">這里的<p>沒有設置className,所以?document.write(p1.className+"<br>");這個語句沒有意義,而且document.write ?是輸出文本語句也沒有意義,想達到改變樣式的效果,就得先給他定義一個className,p1.className = "one";
2016-05-15
需要你改變樣式,所以p1.className = "one";