使用jQuery获取第一个可见元素 [英] Getting first visible element with jQuery

查看:88
本文介绍了使用jQuery获取第一个可见元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用jQuery的:first:visible伪选择器获得列表的第一个可见元素,如此处所示: https://stackoverflow.com/a/830611/165673 ,但无法正常工作:

Trying to get the first visible element of a list using jQuery's :first and :visible pseudo-selectors, as suggested here: https://stackoverflow.com/a/830611/165673 but it's not working:

提琴手: http://jsfiddle.net/FAY9q/4/

HTML:

<ul>
    <li>Item A</li>
    <li>Item B</li>
    <li>Item C</li>
</ul>
<ul>
    <li style="display:none;">Item A</li>
    <li>Item B</li>
    <li>Item C</li>
</ul>

JQUERY:

$('li:visible:first').css('background','blue');

每个列表中的第一项应变成蓝色...

The first item in each list should turn blue...

推荐答案

尝试使用此功能:

$('ul').find('li:visible:first').css('background','blue');

当前,您的代码只是在页面上获得第一个可见的li元素并设置背景色.该代码选择所有ul元素,然后在每个元素中找到第一个可见的li并应用样式.

Currently your code is just getting the first visible li element on the page and setting the background colour. This code selects all ul elements then finds the first visible li within each of them and applies the style.

它在这里工作: http://jsfiddle.net/FAY9q/5/

这篇关于使用jQuery获取第一个可见元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆