function do_reset () {
document.getElementById("txt");
txt.className = "two";
var classnum ;
classnum = txt.className ;
alert(classnum);
}function do_reset() 不起作用
<div id="txt" class="one">
这是父容器,我加了一个类名
.one{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
.two{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}这是我的css样式,考虑到权值(#id是100,.class是10)所以我把原来的选择器换为类的选择器,然后通过更改类名做到切换样式。最后没有重置的效果
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8"/>
<title>javascript</title>
<style type="text/css">
.one{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
.two{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
</style>
</head>
<body>
<div id="txt" class="one"></div>
<input type="button" onclick="do_reset()" value="重置">
<script type="text/javascript">
function do_reset () {
var txt = document.getElementById("txt");
txt.className = "two";
var classnum ;
classnum = txt.className ;
alert(classnum);
}
</script>
</body>
</html>你的代码给的是片段,产不是完整代码,所以不知道是不是你的代码结构有问题。从片段中可以看出来的问题是:我的代码的25 你所用的txt 没有声明出来,也就是说不存在,第24行,你根据id去获取了一个元素,你并没有保存下来,也就是说这一步是白做。你的两个class的样式内容是一样的,所以重置页面也不会有任何变化。只会alert出重置后的className