簡體   English   中英

如何在不使用indexOf方法的情況下定位具有特定內聯樣式的特定dom css類

[英]How can I target a specific dom css class that has a specific inline style without using indexOf method

最初對我有用的答案在這里。
如何選擇具有特定內聯樣式的元素?

$('div').filter(function() {
return $(this).attr('style') == undefined || $(this).attr('style').indexOf('display: none;') == -1
}).

但是,它是Jquery 2+,我想知道是否有更好的方法來實現相同的結果。 對於上述方法,我希望可以更改的是,它需要使用indexOf JS方法來獲得-1或0的值,以實現真實的falesy類型返回。

我已經嘗試過了:但是默認情況下它會選擇第一個樣式。 而且我還沒有找到一種將結果“過濾”到特定類的方法。

$("[style='display: block;']").css("color", "yellow");

我用JSBIN弄清楚了。 是的...我會分享我的答案...

我像上面一樣使用過濾器函數,但是使用.is方法的屬性等於選擇器值。 這是有關jquery屬性等於選擇器的更多信息。 https://api.jquery.com/attribute-equals-selector/

這是最終代碼和Jsbin兩種方法。 讓我知道你的想法。 過濾器函數查找在.bottom-drawer類的“此”文檔中是否存在值為“ display:block”的樣式。 我用style *給它一種類似顯示的模糊方法:block或block;。
http://jsbin.com/coroxa/1/edit?html,js,輸出

$('.bottom-drawer').filter(function() {
    return $(this).is("[style*='display: block']") === true;
}).slideToggle(620);

這是我想出的答案以及相關的bin。

該項目完成的工作是創建div抽屜效果,該效果在另一個抽屜打開時隱藏-另一個關閉。

這並不是說使用indexOf的方法不是一個好選擇,但是對我來說,出於選擇特定類或id的內聯樣式的目的似乎並不直觀。 希望這對某人有幫助。

http://jsbin.com/coroxa/1/edit?html,js,輸出

$('.top.expand-accordian a').click(function(e){
    e.preventDefault();
    $(this).find('i').toggleClass('rotate');
    $('.maui-drawer').slideToggle(620);
    $('.bottom-drawer').filter(function() {
      //return $(this).attr('style').indexOf('display: block;') === 0;
    return $(this).is("[style*='display: block']") === true;
    }).slideToggle(620);
});

$('.bottom.expand-accordian a').click(function(e){
    e.preventDefault();
    $(this).find('i').toggleClass('rotate');
    $('.bottom-drawer').slideToggle(620);
    $('.maui-drawer').filter(function() {
    //;return $(this).attr('style').indexOf('display: block;') === 0;
    return $(this).is("[style*='display: block']") === true;
    }).slideToggle(620);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM