麻烦帮我看一下为什么在浏览器点击按钮后变不了two的样式

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

了吾欲前行

2016-03-19 19:30

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>innerHTML</title>
<style>
    input{
        font-size:10px;
        }
    .one{
        width:500px;
        background-color:red;
    .two{
        font-size:200px;
        color:blue;
        }
</style>
<body>
    <h1>JAVASCRIPT</h1>
        <p id="con" class="one">作为一个优秀的PHP开发师</p>
    <form>
        <input type="button" value="改变类样式" onclick="myClass()">
    </form>
<script type="text/javascript">
    var mychar=document.getElementById("con")
    document.write("p元素的class值为:"+mychar.className+"<br>");
    function myClass(){
        mychar.className="two";
    }
</script>
</body>
</html>

写回答 关注

4回答

  • 深山小童
    2016-03-20 23:35:12

    .one的样式少了个结束的大括号(不起作用的原因),</style>后面少了个</head>结束标签。写代码的时候先将该闭合的闭合,再填代码就不容易出错了。严格来说<input /><br />也是要闭合的。

  • qq_冲哥_0
    2016-03-20 18:53:14

    CSS 样式写错了,

    .one样式后面少了一个大括号, 

    .one{
            width:500px;
            background-color:red;


  • 慕妹9507196
    2016-03-19 20:09:08

    没有给function myClass()函数声明mychar变量的值,应该在{mychar.className="two"前面写上var myclass=document.getElementById("con")}

  • wenqu
    2016-03-19 20:08:23

       var mychar=document.getElementById("con")要放在 

      function myClass(){mychar.className+"<br>")

            mychar.className="two";
        }

    里面;如

    function myClass(){

       var mychar=document.getElementById("con")

            mychar.className="two";
        }

JavaScript入门篇

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

738244 学习 · 9560 问题

查看课程

相似问题