简体   繁体   English

如何让 JQuery 返回有效的 img src?

[英]How can I get the JQuery to return a valid img src?

So, I have images in a div that share the same class and one of them is selected at a time for a single view of that image.因此,我在一个 div 中有共享相同 class 的图像,并且一次选择其中一个用于该图像的单个视图。 In this single view, I have arrows that I want to use to cycle between the images.在这个单一视图中,我想使用箭头在图像之间循环。 The images sharing the same class have sequential alt values.共享相同 class 的图像具有连续的 alt 值。

Right now, I can get the preceding alt value and set the src of the image in the single view to the source of that image.现在,我可以获取前面的 alt 值,并将单个视图中图像的 src 设置为该图像的源。 But, I can't seem to get the same effect with the following image.但是,我似乎无法使用下图获得相同的效果。

    $("#leftButton").click(function(){
            var current= $('img[id="selected"]');
            var altVal=current.attr("alt").valueOf()-1;
            if(altVal>=0)
            {
                var next= $('img[alt='+altVal+']');
                current.attr("id", "");
                next.attr("id", "selected");
                $("#embiggenImage").attr("src", next.attr("src"));
            }
        }
    );

    $("#rightButton").click(function(){
            var gal=$('img[class="galleryImage"]');
            alert(gal.length);
            var current= $('img[id="selected"]');
            var altVal=current.attr("alt").valueOf()+1;
            if(altVal<gal.length)
            {
                alert("inside");
                var next= $('img[alt='+altVal+']');
                current.attr("id", "");
                next.attr("id", "selected");
                $("#embiggenImage").attr("src", next.attr("src"));
            }
        }
    );

As you can see, the code for changing the sources is exactly the same, and it reaches those two alerts fine, but returns a null source.如您所见,更改源的代码完全相同,它可以很好地到达这两个警报,但返回 null 源。

Edit: It seems that in the leftButton click function, alt is being properly decremented, but in the rightButton click function, when I am trying to increment alt, it instead, is appending the '1', such that the final value of altVal is '01'.编辑:似乎在leftButton中单击function,alt正在正确减少,但在rightButton中单击function,当我尝试增加alt时,它反而附加了“1”,这样altVal的最终值为'01'。 This does not make any sense to me..这对我没有任何意义..

The problem is in the difference between these two lines:问题在于这两行之间的区别:

var altVal=current.attr("alt").valueOf()-1;
var altVal=current.attr("alt").valueOf()+1;

- only has one meaning: subtract. -只有一个含义:减法。 If you call it on a string, it will be converted into a number before the operation.如果你在一个字符串上调用它,它会在操作之前转换成一个数字。

+ has two meanings: add and concatenate. +有两个含义:添加和连接。 If you call it on a number, it adds.如果您在一个号码上调用它,它会添加。 If you call it on a string, it concatenates.如果你在一个字符串上调用它,它会连接起来。 attr returns a string (as does valueOf -- I'm not sure what the point of that is), so +1 means "put add 1 to the end of the string". attr返回一个字符串(与valueOf一样——我不确定那是什么意思),所以+1表示“将1添加到字符串的末尾”。

You need to convert the value to a number instead.您需要将值转换为数字。 I'd use the unary + operator :我会使用一元+运算符

var altVal = +current.attr('alt') + 1;

If the final value in the rightButton click event for "alt" is "01", try using parseInt() before adding to it, like so:如果“alt”的 rightButton 点击事件的最终值为“01”,请在添加之前尝试使用parseInt() ,如下所示:

parseInt(current.attr("alt").valueOf())+1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM