它是我的一次。我如何从定义的数组中调用数据?Jquery,与数组一起工作
var myArray = new Array("http://www.gravatar.com/avatar.php", "http://1.gravatar.com/avatar/", "http://0.gravatar.com/avatar/");
$('img[src^="DATA FROM myArray"]').remove()
它是我的一次。我如何从定义的数组中调用数据?Jquery,与数组一起工作
var myArray = new Array("http://www.gravatar.com/avatar.php", "http://1.gravatar.com/avatar/", "http://0.gravatar.com/avatar/");
$('img[src^="DATA FROM myArray"]').remove()
$(myArray).each(function(idx,elem){
/*idx is item index, elem is the item itself*/
$('img[src^="'+elem+'"]').remove();
})
InArray让我们来得到一个数组中项目的索引。所以:
myArray[$.inArray("http://www.gravatar.com/avatar.php",myArray)]
var myArray = [
"http://www.gravatar.com/avatar.php",
"http://1.gravatar.com/avatar/",
"http://0.gravatar.com/avatar/"
];
$('img').filter(function() {
var inArray = false;
var src = $(this).attr('src');
$.each(myArray, function() {
if(src.indexOf(this) == 0)
inArray = true;
}
return inArray;
}).remove()
或者你可以只使用正则表达式:
$('img').filter(function() {
return $(this).attr('src')
.match(/^http:\/\/(www|0|1)\.gravatar\.com\/avatar(\.php)?/i);
}).remove()
你可以做这样的事情:
$('img[src^="' + myArray[1] + '"]').remove();
如果要选择所有元素,独立于你想要做什么和他们在一起,你可以这样做:
var $elements = $();
for(var i = myArray.length;i--;) {
$elements.add($('img[src^="' + myArray[i] + '"]'));
}
您应该使用数组文本[...]
,而不是数组构造。
也许它的速度更快这样获取:
$("img[src]").filter(function() {
return $.inArray($(this).attr("src"), myArray) != -1;
}).remove();
这不会做问题。测试是'src' _starts_是否具有数组元素,而不是_is_数组元素。 – Eric
你是对的,那是真的! –
我相信这个解决方案是simpliest和最好的一个。 –
是的,tnx队友这个例子工作正常:) –
@Mister X:不要忘记在我的答案中使用更清晰的数组文字符号。 – Eric