duanqi6007 2014-12-03 16:43
浏览 52
已采纳

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

I currently have a slew of images that I would like to display after passing certain variables to an external JS file:

HTML Code:

<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:

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.

  • 写回答

1条回答 默认 最新

  • dongting3135 2014-12-03 16:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64