开发者

Jquery, work with arrays

it's me again. How I can call data from defined array?

var myArray = new Array("http://w开发者_运维技巧ww.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();
})


If you want to select all that elements, independent from what you want to do with them, you could do:

var $elements = $();

for(var i = myArray.length;i--;) {
    $elements.add($('img[src^="' + myArray[i] + '"]'));
}

You should use array literals [...] instead of the array constructor.


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()

Or you could just use regex:

$('img').filter(function() {
    return $(this).attr('src')
                  .match(/^http:\/\/(www|0|1)\.gravatar\.com\/avatar(\.php)?/i);
}).remove()


You can do something like this:

$('img[src^="' + myArray[1] + '"]').remove();


Maybe it's faster to fetch like this:

$("img[src]").filter(function() {
  return $.inArray($(this).attr("src"), myArray) != -1;
}).remove();


InArray let's you get an the index of an item within an array. So:

myArray[$.inArray("http://www.gravatar.com/avatar.php",myArray)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜