【问题标题】:How to select an element where attribute opacity is 1 with jQuery如何使用jQuery选择属性不透明度为1的元素
【发布时间】:2012-06-25 14:49:18
【问题描述】:

我有这些元素,我需要选择 ul 中 opacity=1 的 li。 我该怎么做?

<ul class="class" id="ul">
                <li style="width: 100%; list-style: none outside none; position: absolute; top: 0px; left: 0px; z-index: 98; opacity: 0;"><a title="title1" href=""><img alt="alt" class="class_name" src="/images/7dfc294d5c3bcebecb2ec0e44fd27d1c.jpg"></a></li>
                <li style="width: 100%; list-style: none outside none; position: absolute; top: 0px; left: 0px; z-index: 98; opacity: 0;"><a title="title2" href=""><img alt="alt" class="class_name " src="/images/a9c9eb42934df4576b352d88f607f292.jpg"></a></li>
                <li style="width: 100%; list-style: none outside none; position: absolute; top: 0px; left: 0px; z-index: 98; opacity: 0;"><a title="title3" href=""><img alt="alt" class="class_name " src="/images/b64264692c0d648068c9d1380e9099c1.jpg"></a></li>
                <li style="width: 100%; list-style: none outside none; position: absolute; top: 0px; left: 0px; z-index: 99; opacity: 1;"><a title="title4" href=""><img alt="alt" class="class_name " src="/images/43e3e5e2edc4234ecddbc89636e4e224.jpg"></a></li>
                <li style="width: 100%; list-style: none outside none; position: absolute; top: 0px; left: 0px; z-index: 98; opacity: 0;"><a title="title5" href=""><img alt="e-alt" class="class_name " src="/images/31a156ce7f7ab5485366d24f6cbfbede.jpg"></a></li>
            </ul>

【问题讨论】:

    标签: jquery attributes opacity


    【解决方案1】:
    $('#ul li').filter(function() {
      return $(this).css('opacity') == '1';
    });
    

    DEMO

    你也可以试试.each()

    var lis = [];
    $('#ul li').each(function() {
        if ($(this).css('opacity') == '1') {
            lis.push(this);
        }
    });
    

    DEMO

    或使用.map()

    var lis = $('#ul li').map(function() {
        if($(this).css('opacity') == '1')
            return this;
    }).get();
    

    DEMO

    【讨论】:

    • 这是否也适用于 IE 的 filter CSS 规则?设置不透明度可以,但也可以设置吗?
    • 糟糕,我在加载 jQuery 时遇到问题,这就是为什么我有空错误。它有效
    【解决方案2】:

    你可以试试

    $('li[style*="opacity: 1"]')
    

    但我不确定它是否会在没有空间时返回元素,例如opacity:1

    【讨论】:

    • 出现错误:$("li[style*=\"opacity: 1\"]") 为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 2013-07-17
    • 2012-01-01
    • 2011-02-27
    • 2011-05-07
    • 1970-01-01
    相关资源
    最近更新 更多