为什么我这里设置的取消按钮是无效的呢?

来源:3-6 控制类名(className 属性)

qq_牛先森_0

2016-06-21 18:04

<style>

    body{ font-size:16px;}

    .one{

border:1px solid #eee;

width:230px;

height:50px;

background:#ccc;

color:red;

    }


</style>

</head>

<body>

    <p id="p1"> JavaScript使网页显示动态效果并实现与用户交互功能。</p>

    <input type="button" value="添加样式" onclick="add()"/>

    

    <input type="button" value="取消设置" onclick="quxiao()"/>


<script type="text/javascript">

  function add(){

     var p1 = document.getElementById("p1");

     p1.className="one";

  }

//定义取消设置 

      function quxiao(){

        var a = document.getElementById("p1");

        var qx=confirm("是否取消");

        if(qx==true){

        a.removeAttribute('style'); 

    }

}

</script>


写回答 关注

4回答

  • RDS_ray
    2016-06-25 17:18:32

    a.removeAttribute('class');因为它没有style,只有class,你的style是通过class添加的

  • Derek81
    2016-06-22 16:07:46

     a.removeAttribute('style'); 这句是无效的,改为 a.className="";

    因为 var a = document.getElementById("p1");而<p id="p1">中并无style属性。

    样式的添加是通过给对象的className属性赋值实现的,要取消样式就要通过给className赋值为空。

    如果样式添加是通过setAttribute("style","color:red")方法实现的,removeAttribute('style')就起作用了。当然由于setAttribute()方法存在兼容性问题,很少用这种方法实现样式添加。

  • 大胶布
    2016-06-21 20:07:55

      a.removeAttribute('style'); 这句有问题吧,换成a.className="";   就可以了,我也不知道为什么

  • 慕粉3537760
    2016-06-21 19:53:42

    我也不懂。。。。

JavaScript入门篇

JavaScript做为一名Web工程师的必备技术,本教程让您快速入门

738244 学习 · 9560 问题

查看课程

相似问题