返回值:jQueryshow()

显示匹配的元素。

显示元素的最简单方式就是调用不带参数的 .show() 方法:

$('.target').show();

此时元素会被立即显示,不带有任何动画。这基本等价于 .css('display', 'block') ,只不过此时 display 的属性值会保存到 jQuery 的数据缓存中,之后还能恢复到 display 的初始值。比如,如果一个元素的 display 值为 inline,那么当它被隐藏再显示时,它的值仍然是 inline

注意: 如果在样式中使用了 !important, 例如: display: none !important, 需要使用 .css('display', 'block !important'), 确保 .show() 方法能正常工作。

如果提供了 duration 参数,.show() 就变成一个动画方法了。.show() 会同时对元素的高、宽以及透明度进行动画操作。

duration 参数可以提供一个毫秒数,代表动画运行的时间,时间越长动画越慢。还可以提供字符串 'fast''slow' ,分别对应了 200600 毫秒。如果没有设置 duration 参数,或者设置成其他无法识别的字符串,就会使用默认值 400 毫秒。

从 jQuery 1.4.3 起,增加了一个可选的参数,用于确定使用的缓冲函数。缓冲函数确定了动画在不同位置的速度。jQuery默认只提供两个缓冲效果:swing(默认值) 和 线性缓冲效果linear。更多特效需要使用插件。可以访问 jQuery UI 网站 来获得更多信息。

如果提供了回调函数,那么当动画结束时,会调用这个函数。通常用来按顺序执行一组不同的动画。这个函数不接受任何参数,但是 this 会设成将要执行动画的那个元素。如果对多个元素设置动画,那么要非常注意,回调函数会在每一个元素执行完动画后都执行一次,而不是这组动画整体才执行一次。

我们可以对任何元素应用动画,比如下面这个例子,对图片应用动画:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />
With the element initially hidden, we can show it slowly:
$('#clickme').click(function() {
  $('#book').show('slow', function() {
    // Animation complete.
  });
});

补充说明:

  • 所有的 jQuery 动画, 包括 .show(), 都可以被关闭,通过全局设置 jQuery.fx.off = true, 效果等同于将动画时间 duration 设置成 0. 可以访问 jQuery.fx.off 来获得更多信息。

示例:

在600毫秒内(字符串常量为"slow"),将所有隐藏的段落显示出来。

<!DOCTYPE html>
<html>
<head>
<style>
      p { background:yellow; }
      </style>
<script src="jquery.min.js"></script>
</head>
<body>

<button>Show it</button>

      <p style="display: none">Hello  2</p>

<script>


    $("button").click(function () {
    $("p").show("slow");
    });
    

</script>
</body>
</html>

演示:

示例:

依次显示 div,每个用时 200 毫秒。一个动画完成后立即开始下一个。

<!DOCTYPE html>
<html>
<head>
<style>
  div { background:#def3ca; margin:3px; width:80px;
  display:none; float:left; text-align:center; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>


  <button id="showr">Show</button>
  <button id="hidr">Hide</button>
  <div>Hello 3,</div>

  <div>how</div>
  <div>are</div>
  <div>you?</div>

<script>


$("#showr").click(function () {
  $("div").first().show("fast", function showNext() {
    $(this).next("div").show("fast", showNext);
  });
});

$("#hidr").click(function () {
  $("div").hide(1000);
});


</script>
</body>
</html>

演示:

示例:

动画显示所有 span 和 input 元素。可以试着在下面的文本框中输入 yes 并按回车。

<!DOCTYPE html>
<html>
<head>
<style>
  span { display:none; }
  div { display:none; }
  p { font-weight:bold; background-color:#fcd; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>


<button>Do it!</button>
<span>Are you sure? (type 'yes' if you are) </span>
<div>
  <form>
    <input type="text"  value="as;ldkfjalsdf"/>
  </form>
</div>
<p style="display:none;">I'm hidden...</p>


<script>


function doIt() {
  $("span,div").show("slow");
}
/* can pass in function name */
$("button").click(doIt);

$("form").submit(function () {
  if ($("input").val() == "yes") {
    $("p").show(4000, function () {
      $(this).text("Ok, DONE! (now showing)");
    });
  }
  $("span,div").hide("fast");
  /* to stop the submit */
  return false;
});


</script>
</body>
</html>

演示: