<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 参数)
没有 AJAX
<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 参数)
没有 AJAX
如果希望它是动态的,以便使用单击时变量的值,请执行以下操作:
<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 处理程序。
将 id 属性放在锚元素上
<a id="link2">
在页面加载事件上设置 href 属性:
(function() {
var scrt_var = 10;
var strLink = "2.html&Key=" + scrt_var;
document.getElementById("link2").setAttribute("href",strLink);
})();
或者,您可以只使用 document.write:
<script type="text\javascript">
var loc = "http://";
document.write('<a href="' + loc + '">Link text</a>');
</script>
<script>
var scrt_var = 10;
document.getElementById("link").setAttribute("href",scrt_var);
</script>
<a id="link">this is a link</a>
<html>
<script language="javascript" type="text/javascript">
var scrt_var = 10;
openPage = function() {
location.href = "2.html?Key="+scrt_var;
}
</script>
this is a <a href ="javascript:openPage()">Link </a>
</html>