2012-11-01 73 views
0

可能重複:
How to check if jQuery object exist in array?的jquery inArray()

有任何方法,以確定是否一個DOM元素是在DOM元素的數組?

$(".container img").each(function(){ 
    views.push($(this).clone()); 
}); 

創建克隆圖像元素的陣列。我現在想檢查一個新的圖像元素是否在該數組中?

感謝

+0

'views.indexOf(yourImage)!== -1' –

+0

你不喜歡['inArray()'](http://api.jquery.com/ jQuery.inArray /)?你已經寫在標題... – Christoph

回答

2
$(".container img").each(function(){ 
    if(jQuery.inArray($(this), views) === -1) 
    { 
     views.push($(this).clone()); 
    } 
}); 

或檢查How to check if jQuery object exist in array?其他的解決方案。

供您使用檢查http://jsfiddle.net/hxAg4/34

+0

@Christoph抱歉,我從textwrangler的副本無法正常工作。固定。 – Bot

+0

你能告訴我爲什麼這不起作用嗎? http://jsfiddle.net/hxAg4/18/ –

+0

@Seglespaan你可以用你的代碼和html創建一個http://jsfiddle.net嗎? – Bot