切换 jQuery,隐藏第一个段落

toggle in jQuery, hide paragraph in first

正如我在标题中提到的,我发现了一个 jQuery 函数来创建一个按钮来隐藏和显示一个段落 html 但是,当我进入我的页面时,我希望我的 pragraph 是隐藏,现在我有逆..段落从头开始显示

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").toggle();
    });
});
</script>
<body>
    <button>Toggle between hiding and showing the paragraphs</button>
 <p>
   If you see me it is that you had to click on the button
</p>
</body>

只需应用 hidden 属性,即可默认隐藏该段落

$(document).ready(function() {
  $("button").click(function() {
    $("p").toggle();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button>Toggle between hiding and showing the paragraphs</button>
<p hidden>
  If you see me it is that you had to click on the button
</p>

在定义后 trigger 单击事件处理程序就足够了。

$("button").click(function(){
    $("p").toggle();
}).trigger('click');
//^^^^^^^^^^^^^^^^^^
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button>Toggle between hiding and showing the paragraphs</button>
<p>
    If you see me it is that you had to click on the button
</p>