gpt4 book ai didi

jquery - 隐藏第 n 个元素之后的所有元素

转载 作者:技术小花猫 更新时间:2023-10-29 12:09:33 24 4
gpt4 key购买 nike

4号之后<a>元素找到了,我怎么会.hide()其余的部分?以下是我到目前为止编写的代码:

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
$(document).ready(function() {
if($('a').length >= 4) {
window.alert('4 or more');
}
});
</script>

<a>test </a><br>
<a>fed </a><br>
<a>fdes </a><br>
<a>grr </a><br>
<a>rerf </a><br>
<a>dferf </a>

有什么想法吗?

最佳答案

使用:gt(index)选择器:

$('a:gt(3)').hide();

或者更快slice功能:

$('a').slice(4).hide();

Live DEMO

Because :gt() is a jQuery extension and not part of the CSS specification, queries using :gt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(index) instead.

关于jquery - 隐藏第 n 个元素之后的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com