recherche

Maison  >  Questions et réponses  >  le corps du texte

javascript - js赋值style元素内容

<head >
    <title></title>
    <style id="syle_id" type="text/css">
        .class1
        {
            color: Red;
        }
    </style>
    <script>
        function click_span() {
            document.getElementById("syle_id").innerHTML = ".class1{color:blue} ";
        }
    </script>
</head>
<body>
    <p>
        <span class="class1" onclick="click_span()">1</span>
    </p>
</body>

贴上源码,欲实现点击后样式发生改变 google是ok的,ie下报未知错误,求指点

阿神阿神2896 Il y a quelques jours641

répondre à tous(2)je répondrai

  • PHPz

    PHPz2017-04-10 12:50:59

    找到ie的赋值方式 document.getElementById("syle_id").styleSheet.cssText = ".class1{color:blue}"; 谷歌直接就innerhtml

    répondre
    0
  • PHPz

    PHPz2017-04-10 12:50:59

    话说你为什么不直接用

        function click_span() {
            var class1 = document.getElementsByClassName('class1');
            for(var i in class1) { class1[i].style.color = 'blue';
        }
    

    而要用这么奇葩的方式?


    认真回答一下吧:一般碰上这种情况,都是预先将改变前和改变后的样式分别以两个class或者别的标记预先写到css中,使用的时候只要替换标记就好了,而不是你这种等到要用的时候现加。

    <style type="text/css">
        .class1 { color:red; }
        .class1.clicked { color:blue; }
    </style>
    <span class="class1" onclick="this.className += ' clicked'">1</span>
    

    répondre
    0
  • Annulerrépondre