==在我的IF语句中不起作用 [英] The == is not working in my IF statement Function

查看:45
本文介绍了==在我的IF语句中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的功能(向上),在这里它的箭头和onclick应该在div"gallerymain"中检查图像1,如果在其中应该显示图像2.否则显示图像3,并且应该显示图片4.

i have a simple function (up) here its an arrow and onclick is should check for image 1 in the div "gallerymain" and if its there it should show image 2. ELSE if image 3 is displayed and it suppose to show image 4 .

但是由于某种未知的原因==不能正常工作(我用alert进行了检查),我还确保所有情况都在正确的大小写和路径下.有趣的是,如果我将使用=而不是==可以工作,但是onclick它将显示图像2,无论如何(即使存在图像3)我都知道为什么(因为我将路径分配给src).也许有人请重新看一下,看看我想念的是什么.

however for some unknown reason the == isn't working (i checked with alert ) , i also made sure that all is in the right case and path . the funny thing is that if i will use = instead of == it will work but it onclick it will show image 2 no matter what (even if image 3 is there ) i understand why (because i am assigning the path to the src . maybe somebody please take a fresh look on it and see what am i missing.

谢谢!

  function up () {         


     if 
        (document.getElementById("gallerymain").src == 
    "../images/gallery/image1.jpg") 
        {
        document.getElementById("gallerymain").src = 
    "../images/gallery/image2.jpg"
        }
    else if   
    (document.getElementById("gallerymain").src == 
    "../images/gallery/image3.jpg") 
        {
    document.getElementById("gallerymain").src = 
    "../images/gallery/image4.jpg"
        }
    }

推荐答案

img.src getter (如 img.src =='../img.jpg')返回绝对URL ,即 http://mysite.dev/img.jpg . img.src 设置(如 img.src ='../img.jpg'中)修改相对URL 绝对.
getAttribute 方法完全返回已分配的内容.所以试试这个.

img.src getter (as in img.src == '../img.jpg') returns absolute URL that is http://mysite.dev/img.jpg. img.src setter (as in img.src = '../img.jpg') amends relative URL to absolute.
getAttribute method returns exactly what was assigned. So try this.

if 
    (document.getElementById("gallerymain").getAttribute('src') == 
"../images/gallery/image1.jpg") 
    {
    document.getElementById("gallerymain").src = 
"../images/gallery/image2.jpg"
    }

这篇关于==在我的IF语句中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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