Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

javascript - 将Javascript变量传递给<a href>(Passing Javascript variable to <a href >)

<script language="javascript" type="text/javascript">
var scrt_var = 10; 
</script>

HTML Part:

(HTML部分:)

<html>
 this is a <a href ="2.html & Key= scrt_var">Link  </a>
</html>

I just want to sent the javascript variable to link (url parameter)

(我只想将javascript变量发送到link(url参数))

No AJAX

(没有AJAX)

  ask by joe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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.

(你应该有一个脚本,在DOM加载后为所有相关的<a>元素添加一个onclick处理程序。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...