【问题标题】:Show first four children with jQuery使用 jQuery 显示前四个孩子
【发布时间】:2012-01-15 22:59:39
【问题描述】:

我有一个<div>,在那个 div 中有几个<a> 标签。使用 jQuery,我想显示前四个 <a> 标签。

我已经用下面的代码做到了:

$('div.gallery-hor.read-more a:nth-of-type(-1n+4)').show();

有一个问题:IE* 及更高版本不支持:nth-of-type() 代码。

是否有解决方法宽度 jQuery 来解决这个问题跨浏览器?

【问题讨论】:

标签: jquery


【解决方案1】:

http://docs.jquery.com/Selectors/lt#index

$('div.gallery-hor.read-more a:lt(4)').show();

【讨论】:

  • jQuery 状态 Because :lt() is a jQuery extension and not part of the CSS specification, queries using :lt() 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(0, index) instead.
【解决方案2】:

您不仅可以使用slice 选择前四个,还可以从任何索引(开始)到任何索引(结束)进行选择。 EXAMPLE

$('div.gallery-hor.read-more a').slice(0,4).show();

documentation

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2011-01-11
    • 2018-08-14
    • 2012-05-09
    相关资源
    最近更新 更多