简体   繁体   English

通过HTML中的onClick将变量传递到外部js文件

[英]Pass variable to external js file via onClick in HTML

I want to be able to pass a variable to a JavaScript function. 我希望能够将变量传递给JavaScript函数。 with the below onClick event. 下面的onClick事件。 Basically every time the onClick is triggered for these images a var is expected to be sent to a the JavaScript function. 基本上,每次为这些图像触发onClick时,都希望将var发送给JavaScript函数。 So if image one is clicked I want to send: 因此,如果单击图片一,我想发送:

var myvar = "sof273561"; to gotowebpage() 转到gotowebpage()

gotowebpage() function is in show_details.js file. gotowebpage()函数位于show_details.js文件中。

<div class="furnitureTypes">
<!--- furniture image 1 -->
<li><div class="furniture"><input type="image" src="images/furniture/1.jpg onClick="gotowebpage();"/><gt_descA>Sofas</gt_descA><gt_descB>$2050</gt_descB><gt_descC><a href="sofas_27356.pfd" target="_blank" style="color:#FF0000">Check catelog</a></gt_descC></div></li>
<!--- furniture image 2 -->
<li><div class="furniture"><input type="image" src="images/furniture/2.jpg onClick="gotowebpage();"/><gt_descA>Chairs</gt_descA><gt_descB>$420</gt_descB><gt_descC><a href="chairs_18119.pfd" target="_blank" style="color:#FF0000">Check catelog</a></gt_descC></div></li>
<!--- furniture image 3 -->
<li><div class="furniture"><input type="image" src="images/furniture/3.jpg onClick="gotowebpage();"/><gt_descA>Tables</gt_descA><gt_descB>$1300</gt_descB><gt_descC><a href="tables_15723.pfd" target="_blank" style="color:#FF0000">Check catelog</a></gt_descC></div></li>
</div>
</div>
</div>
<script src="js/show_details.js"> </script>

It's slightly unclear how you have written your question. 不清楚您是如何编写问题的。 But you should just be able to pass these values into your gotowebpage function. 但是您应该只能够将这些值传递到gotowebpage函数中。

onClick="gotowebpage('sof27356');"

Then in your show_details.js file. 然后在show_details.js文件中。

var gotowebpage = function(value){
    //do stuff with value
}

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

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