问答详情
源自:9-1 认识DOM

用button隐藏和显示 怎么实现不了 请指教

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>style样式</title>

</head>

<body>

  <h2 id="con">I love JavaScript</H2>

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

  <script type="text/javascript">

  var mychar=document.getElementById("con");

  mychar.style.color="red";

  mychar.style.backgroundColor="#CCC";

  

  function yin(){

      mychar.style.display="none";

  }

  

      function xianshi(){

          

       mychar.style.display="block";

   }

   


  </script>

  <form>

  <input type="button" name="xianshi" value="xianshi" onclick="xianshi()"/>

  <input type="button" name="yin" value="yin" onclick="yin()"/>

 </form>

 


</body>

</html>


提问者:慕婉清917633 2018-08-17 21:34

个回答

  • 慕神2178781
    2018-08-18 10:03:16
    已采纳

    问题出在form表单,把form去掉就行

  • qq_夏佐_0
    2018-11-29 12:47:55

    看我代码哈:


    <!DOCTYPE HTML>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>style样式</title>

    </head>

    <body>

      <h2 id="con">I love JavaScript</h2>

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

      <script type="text/javascript">

       

       var conValue = document.getElementById("con");

       

       function setColor() {

           conValue.style.color = "red";

           conValue.style.backgroundColor.color = "#CCC";

       }

       

       function styleHide() {

           conValue.style.display = "none";

       }

       

       function showValue() {

           conValue.style.display = "block";

       }

     

      </script>

      

      <form> 

        <input type="button" value="改变颜色" onclick="setColor()" />

        <input type="button" value="点击隐藏" onclick="styleHide()" />

        <input type="button" value="点击显示" onclick="showValue()">

      </form>

      

    </body>

    </html>


  • 幸会why
    2018-10-17 19:03:44

    你这个Id"con"没有写在input 里面啊