我的程序中有这一行(在 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 语句?
发布评论