在jQuery中切换,首先隐藏段落

时间:2017-05-09 21:04:56

标签: javascript jquery html display

正如我在标题中提到的那样,发现了一个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>

2 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
$(document).ready(function() {
  $("button").click(function() {
    $("p").toggle();
  });
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

&#13;
&#13;
$("button").click(function(){
    $("p").toggle();
}).trigger('click');
//^^^^^^^^^^^^^^^^^^
&#13;
<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>
&#13;
&#13;
&#13;