2012-06-23 12 views
3

我有這些元素,我需要選擇ul內的li,其中opacity = 1。 我該怎麼做?如何使用jQuery選擇屬性不透明度爲1的元素

<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> 

回答

11
$('#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

+0

這是否工作用IE的'filter' CSS規則呢?設置不透明度確實有效,但是也能起作用嗎? – Martijn

+0

錯誤:$(「#ul li」)爲空 – themis

+0

@themhz你檢查我的演示的控制檯,它的工作.. – thecodeparadox

0

您可以嘗試

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

,但我不知道這是否會返回的元素時,有沒有空間,像opacity:1

+0

它SAIS錯誤:$(「li [style * = \」opacity:1 \「]」)爲空 – themis

+0

即使我糾正了jQuery – themis

相關問題