我正在使用内部 jQuery 创建一个随机 html 页面,我试图让 jQuery 隐藏三个段落中的两个,并执行此操作 5 秒,但是当我加载 html 文件时,所有段落都立即可见。有人可以帮忙吗?
<html>
<head>
<script src="jquery-1.9.1.js"></script>
</head>
<body>
<script>
var $ = jQuery;
$("p").each(function (idx) {
if(idx >= 1) {
$(this).hide(500);
}
});
</script>
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>
</body>
</html>
最佳答案
您必须用 $()
包装代码,因为元素尚未加载。
$(function(){
$("p:not(:first-child)").hide(5000);
});
此外,我相信 500
是一个拼写错误,因为 5000
是 5 秒。
如@David Thomas建议,您可以进一步简化为:
$(function(){
$("p:gt(0)").hide(5000); //:gt means "greater than..."
});
关于javascript - jQuery,隐藏段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605352/