如何为特定图像源制作 If 语句? [英] How to Make If statement for a specific Image source?

查看:35
本文介绍了如何为特定图像源制作 If 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有这一行(在 Dreamweaver 中运行),如下所示:

I have this line in my program (Run in Dreamweaver) which is the following:

function go() {
if((document.test.test1.src == document.test.test2.src && document.test.test2.src == document.test.test3.src ))

......
......
.....
}

当我有 3 个不同的图像时运行此函数,当页面中的三个图像彼此相等时.但是,我将如何指定哪些图像彼此相等,而不是仅执行三项中的任何一项.

This function runs when I have 3 different images which when either of the three equal each other in the page. However, how would I make it to specify which images to equal each other, instead of just doing any of the three.

例如,如果一张图片的标题为x",如果所有三个 test1、test2、test 的值都为x",它将运行该函数并显示此消息,但是,它会为其他图像显示相同的消息(如果都相等 'y') 我想以某种方式有单独的消息.

For ex, if one image is titled 'x' if all three test1,test2,test are of value 'x' it will run the function and display this message, however, it displays the same message for the other images (If all equal 'y') Id like to have separate messages somehow.

推荐答案

我会将比较与单独的函数分开,以当前图像元素为参数,并使用此方法处理不同的图像对象:

I'd separate the comparison to a separate function with current image element as parameter and use this method with different image objects:

function compare(imgElem){
  if((document.test.test1.src == document.test.test2.src && document.test.test2.src == document.test.test3.src ))
    console.log(imgElem.id,"triggered");
    // or something else
  }
}

compare(document.test.test1);
compare(document.test.test2);
compare(document.test.test3);

但是根据您要比较的元素数量,解决方案可能不准确.对于大量元素,请尝试使用地图.

However the solution might not be accurate depending on how many elements you're comparing. For large number of elements, try using a map.

这篇关于如何为特定图像源制作 If 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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