2012-11-01 32 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