简体   繁体   English

将变量传递给外部Javascript文件以通过onClick更改图像

[英]Passing Variable To External Javascript File To Change Images Via onClick

I currently have a slew of images that I would like to display after passing certain variables to an external JS file: 目前,在将某些变量传递到外部JS文件后,我想显示很多图像:

HTML Code: HTML代码:

<li><a href="#" onClick="javascript: update_region('ne');">Northeast</a>
             <ul>
                <li><a id="t2m" href="#" onClick="javascript: update_variable('t2m');">2m Temperature</a></li>
                <li><a id="td2m" href="#" onClick="javascript: update_variable('td2m');">2m Dew Point Temp</a></li>
                <li><a id="dewdep" href="#" onClick="javascript: update_variable('dewdep');">2m Dew Point Depression</a></li>
                <li><a id="wetbulb" href="#" onClick="javascript: update_variable('wetbulb');">2m Wet Bulb Temp</a></li>
                <li><a id="rh" href="#" onClick="javascript: update_variable('rh');">2m Relative Humidity</a></li>
                <li><a id="wind" href="#" onClick="javascript: update_variable('wind');">10m Wind Speed</a></li>
                <li><a id="windgust" href="#" onClick="javascript: update_variable('windgust');">10m Wind Gust</a></li>
                <li><a id="apparent" href="#" onClick="javascript: update_variable('apparent');">2m Apparent Temp</a></li>
            </ul>
        </li>

Javascript Code: JavaScript代码:

function update_region(input) {
window.region = input;
imageload(); };

function update_variable(input) {
window.variable = input;
imageload(); };

function imageload() {
document.imageplot.src=(/images/rtma2p5_'+region+'_'+variable+'.png'); };

I'm not sure if this is an effective way to pass variables from outside the JS file to define the image url code or not, as I can't seem to get this to work at all. 我不确定这是否是从JS文件外部传递变量以定义图像url代码的有效方法,因为我似乎根本无法使它正常工作。

Try ... 尝试...

function imageload() {
  document.imageplot.src=("/images/rtma2p5_" + window.region + "_"+ window.variable + ".png");
};

You were missing a quote and were not referencing the variables created in previous functions correctly. 您缺少引号,并且未正确引用在先前函数中创建的变量。 you might also want to consider some default values. 您可能还需要考虑一些默认值。

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

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