如何在JS中获取ID为div的动作

时间:2019-02-13 15:24:15

标签: javascript jquery html html5

我必须用p隐藏一个id=2,但是我没发现pid=2都不会被隐藏! 我的错误是什么

代码:

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $("#hide").click(function(){
        $("p.2").hide();
      });
      $("#show").click(function(){
        $("p.2").show();
      });
    });
    </script>
    </head>
    <body>
 <p id="2">If you click on the "Hide" button, I will disappear.</p>
    <button id="hide">Hide</button>
    <button id="show">Show</button>
    </body>

2 个答案:

答案 0 :(得分:1)

您正试图显示和隐藏类别为2的<p>元素,但您只有一个{em> ID 2的<p>元素。应该是这样的:

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $("#hide").click(function(){
        $("p#2").hide();
      });
      $("#show").click(function(){
        $("p#2").show();
      });
    });
    </script>
    </head>
    <body>
 <p id="2">If you click on the "Hide" button, I will disappear.</p>
    <button id="hide">Hide</button>
    <button id="show">Show</button>
    </body>

答案 1 :(得分:0)

这是使用 class TForm

搜索元素p
2

应该是$("p.2").hide(); id

#