不会在jQuery中迭代数组 [英] Won't Iterate Over Array In jQuery

查看:121
本文介绍了不会在jQuery中迭代数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我可以把我想要的所有图像都放到一个数组中并将它们传递给$ image。但是,当我尝试遍历该数组时,它只会保持同一项目的警报3次。

So I can get all of the images I want into an array and pass them to $image. However when I try to loop over that array it just keeps alerting the same item 3 times.

我遇到问题的代码。

   getItem : function($image){
    console.log($image)
    console.log(jQuery.type($image))
    var setup ='<img src="' + $($image).attr('href') + '" title="' +    $($image).attr('title') + '"/>';

    $.each($image, function(i){
        alert( setup);
    });

 } 

HTML

    <a href="images/slideshow/1-GW.PhillipBarnhart.ReverendMemory.jpg" title="Phillip Barnhart as: 
        Reverend Memory - a clergyman who stands for decorum and truth." rel="slideshow"><img src="images/view-slideshow.jpg" width="490" height="352" alt="View Slideshow"></a>
        <a rel="slideshow" href="images/slideshow/2-GW.BethBrooks.POLLYTODD.jpg">fff</a>
        <a rel="slideshow" href="images/slideshow/3-GW.NickHale.NOSTALGIA.jpg">test</a>

整个脚本或者如果你喜欢jsFiddle这里是一个链接。 http://jsfiddle.net/h3az4/

The whole script or if you like jsFiddle here is a link. http://jsfiddle.net/h3az4/

var slideShow = {
config : {
    wrapper : 'body',
    container : 'div',
    anchor : 'a[rel="slideshow"]'
},

init : function(config) {
    $.extend(slideShow.config, config);
    $(slideShow.config.anchor).hide();
    $(slideShow.config.wrapper).find(slideShow.config.anchor)
        .eq(0)
        .show()
        .click(function(e){
            e.preventDefault();
            slideShow.getItem($(slideShow.config.anchor));
        });
},

getItem : function($image){
    console.log($image)
    console.log(jQuery.type($image))
    var setup ='<img src="' + $($image).attr('href') + '" title="' + $($image).attr('title') + '"/>';

    $.each($image, function(i){
        alert( setup);
    });


},

createTumbnail : function($image){

}

};


$(document).ready(function() {
slideShow.init();
 });


推荐答案

您使用$ .each循环错误。

Your using the $.each loop wrong.

你的第一个问题是,如果$ image是一个列表,$ image.attr(x)将得到列表中第一个元素的attr。你想要的是$($ image [i])或者使用 .get

Your first problem is that $image.attr("x") will get the attr of the first element in the list if $image is a list. What you want is either $($image[i]) or using .get

第二个问题是声明循环外的 var setup 。这意味着它宣布和使用一次而不是3次(因为你有3个项目)。

The second issue is declaring var setup outside the loop. Which means its declared and used once rather then 3 times (since you have 3 items).

$.each($image, function(i){
    var setup ='<img src="' + $(this).attr('href') + '" title="' +       
        $(this).attr('title') + '"/>';
    alert( setup);
});

当您使用 $。每个 函数中的这个对象将依次引用数组中的每个对象。在这种情况下这个是一个DOM对象,所以你想使用 $(this)来获取jQuery图像对象。

When your using $.each the this object in the function will refer to each object in the array in turn. In this case this is a DOM object so you want to use $(this) to get the jQuery image object.

在这里查看一个工作示例 http:/ /jsfiddle.net/Raynos/h3az4/3/

Look here for a working example http://jsfiddle.net/Raynos/h3az4/3/

这篇关于不会在jQuery中迭代数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆