現在有這樣一種需求,需要選出所有有背景圖片的元素. 
這個問題有點棘手.我們無法使用選擇表達式來完成這個問題了. 
使用jQuery的DOM過濾方法filter(),可以根據函數中表達的任何條件選擇元素. 
jQuery中的過濾器方法允許傳遞一個字符串(也就是選擇器表達式)作為參數. 
或者傳遞的是一個函數.它的返回值將定義某個元素是否被選中. 
傳遞的函數將對當前選擇集中的每個元素運行. 
當函數返回假時,對應的函數就從選擇集中被刪除掉.每當返回值為真的時候,對應的元素 
不受影響. 
復制代碼 代碼如下:
 
jQuery('*').filter(function(){ 
return !!jQuery(this).css('background'); 
}); 
復制代碼 代碼如下:
 
jQuery('div').filter(function(){ 
var width = jQuery(this).width; 
return width >100 && widht < 200; 
}); 
//返回子元素有10個或者20個的元素. 
jQuery('*').filter(function(){ 
var children = jQuery(this).childern().length; 
return children ===10 || children ===20; 
}); 
復制代碼 代碼如下:
 
<html> 
<head> 
<title></title> 
<style type="text/css"> 
.c1{ 
background-color: yellow; 
} 
.c2{ 
background-color: green; 
} 
p{ 
background-color: pink; 
} 
h3{ 
background-color: gray; 
} 
</style> 
</head> 
<body> 
<div>Bye Bye Beautiful</div> 
<div>Nothing but the girl</div> 
<p>The Lazy song</p> 
<h2>If I die young</h2> 
<h3>New soul</h3> 
<script type="text/javascript" src="jquery.2.0.3.js"></script> 
<script type="text/javascript"> 
jQuery(document).ready(function($) { 
var ret = $('*').filter(function(index) { 
return !$(this).css('background-color'); 
}); 
$.each(ret, function(index, val) { 
$(val).css('background-color','black'); 
}); 
}); 
</script> 
</body> 
</html> 
新聞熱點
疑難解答
圖片精選