将Javascript变量传递给< a href> [英] Passing Javascript variable to <a href >
本文介绍了将Javascript变量传递给< a href>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script language="javascript" type="text/javascript">
var scrt_var = 10;
</script>
HTML部分:
<html>
this is a <a href ="2.html & Key= scrt_var">Link </a>
</html>
我只想将javascript变量发送到链接(url参数)
I just want to sent the javascript variable to link (url parameter)
没有AJAX
推荐答案
如果你想要它是动态的,那么变量的值在使用点击时,请执行以下操作:
If you want it to be dynamic, so that the value of the variable at the time of the click is used, do the following:
<script language="javascript" type="text/javascript">
var scrt_var = 10;
</script>
<a href="2.html" onclick="location.href=this.href+'?key='+scrt_var;return false;">Link</a>
当然,这是快速而肮脏的解决方案。你应该有一个脚本,在DOM加载后为所有相关的< a>
元素添加一个onclick处理程序。
Of course, that's the quick and dirty solution. You should really have a script that after DOM load adds an onclick handler to all relevant <a>
elements.
这篇关于将Javascript变量传递给< a href>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文