天天看点

Jquery div 的隐藏和显示

jquery的显示和隐藏

一、jquery的 hide() 和 show()

hide() 隐藏 , show() 显示

<!DOCTYPE html>

<html>

<head>

</head>

<body>

<p id="p1">如果点击“隐藏”按钮,我就会消失。</p>

<button id="hide" type="button">隐藏</button>

<button id="show" type="button">显示</button>

</body>

</html>

<script src="/jquery/jquery-1.11.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

  $("#hide").click(function(){

  $("#p1").hide();

  });

  $("#show").click(function(){

  $("#p1").show();

  });

});

</script>

二、jquery的 toggle()

toggle() 可以来回切换 hide() 和 show() 方法

显示被隐藏的元素,并隐藏已显示的元素:

<!DOCTYPE html>

<html>

<head>

<script src="/jquery/jquery-1.11.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

  $("button").click(function(){

  $("p").toggle();

  });

});

</script>

</head>

<body>

<button type="button">切换</button>

<p>这是一个段落。</p>

<p>这是另一个段落。</p>

</body>

</html>