2013-02-06 30 views
0

我這裏有jQuery的,如果我的格是空

<script type="text/javascript"> 
$(document).ready(function() { 
    if($('.subhead').is(':empty')){ 
     $(this).css('background-color', 'none !important'); 
    } 

}); 
</script> 

這個劇本我想在這裏做的是說,如果我的課.subhead爲空,則改變背景顏色,我在多個.subhead DIV代碼,如果我像這樣運行

<script type="text/javascript"> 
    $(document).ready(function() { 
     if($('.subhead').is(':empty')){ 
      alert('hi'); 
     }else{ 
      alert('hello'); 
     } 
    }); 
    </script> 

一個腳本,它將返回你好

是什麼,我試圖做可能嗎?

回答

5

默認設置background-colourtransparent而不是none。試試這個:

if ($('.subhead').is(':empty')){ 
    $(this).css('background-color', 'transparent !important'); 
} 
+0

是的,這是一個很好的發現...背景:無;或背景顏色:透明; – jasonflaherty

2

使用:empty的選擇,因爲你有幾個.subhead元素,使用is沒有做你所期望的。您的代碼表示,如果所有類別爲.subhead的匹配元素都爲空,則將所有匹配元素的background-color屬性設置爲none

$('.subhead:empty').css('background-color', 'none !important'); 
相關問題