为了账号安全,请及时绑定邮箱和手机立即绑定

重置不起作用,附代码

   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)所以我把原来的选择器换为类的选择器,然后通过更改类名做到切换样式。最后没有重置的效果

正在回答

代码语言

1 回答

<!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

0 回复 有任何疑惑可以回复我~
#1

慕侠2950284 提问者

好的,谢谢,我会根据你的建议修改
2015-11-26 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

重置不起作用,附代码

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号