<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>控制类名(className属性)</title> </head> <style style = "text/css"> body{ font-size:20px; color:red; /*background-color:#666;*/ } .one{ width:200px; background-color:#666; } .two{ font-size:12px; color:#F00; } </style> <body> <p id = "con" class = "one">JavaScript使网页显示动态效果并实现与用户交互功能。</p> <form> <input type ="button" onclick ="modifyclass()" value ="点击更改属性" > <input type ="button" onclick ="modify1()" value ="点击显示更改后的属性" > </form> <script type ="text/javascript"> var myclass = document.getElementById("con"); document.write("P元素的Class值为:"+myclass.className+"<br>"); function modifyclass(){ myclass.className = "two"; } function modify1(){ document.write("改变后P元素的Class值是:"+myclassName); } </script> </body> </html>
我的问题是:想显示更改后P元素的className属性的值,为什么点击“显示更改后的属性”按钮时没有反应呢?
<script type ="text/javascript"> var myclass = document.getElementById("con"); function modifyclass(){ myclass.className = "two"; document.write("P元素的Class值为:"+myclass.className+"<br>"); } function modify1(){ myclass.className = "two"; document.write("改变后P元素的Class值是:"+myclass.className); } </script>
function modify1(){
myclass.className = "two";
document.write("改变后P元素的Class值是:"+myclass.className);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>控制类名(className属性)</title>
</head>
<style style = "text/css">
body{
font-size:20px;
color:red;
/*background-color:#666;*/
}
.one{
width:200px;
background-color:#666;
}
.two{
font-size:12px;
color:#F00;
}
</style>
<body>
<p id = "con" class = "one">JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<form>
<input type ="button" onclick ="modifyclass()" value ="点击更改属性" >
<input type ="button" onclick ="modify1()" value ="点击显示更改后的属性" >
</form>
<script type ="text/javascript">
var myclass = document.getElementById("con");
function modifyclass(){
myclass.className = "two";
}
function modify1(){
myclass.className="one";
}
</script>
</body>
</html>
这样好像有反应不知道对不对
函数modify1()里 myclassName没定义,你是想写这个的吧myclass.className。