簡體   English   中英

使用jQuery檢測內聯背景樣式

[英]detect inline background style with jQuery

我有一個<div> ,有時會應用一個內聯background屬性。

我希望能夠檢查是否有內聯樣式,如果有,那么我要繼續並附加一個background-position

有沒有一種方法可以明確檢查應用於( $(this)內聯背景?

if ($(this).css('background').length > 0) { 
   console.log('has a background'); 
   // set a background position
   $(this).css('background-position', 'center 22px');
}

我敢打賭這樣的事情可能會起作用

if($(this).attr("style").indexOf("background") >=0){
  //we have inline background styles!
}

您可以使用prop測試內聯樣式

if ($(this).prop('style').backgroundImage) {
  //.. do what you want to
}

暫無
暫無

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

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