Javascript 通过href链接发送JS变量
我正在尝试向特定网页发送一个JS变量。下面是我的代码Javascript 通过href链接发送JS变量,javascript,html,href,Javascript,Html,Href,我正在尝试向特定网页发送一个JS变量。下面是我的代码 <script> function hello() { var data="hello"; <a href="www.mytestsite.com?var="' +data +'>Send now</a> } </script> 函数hello() { var data=“hello”; } 当用户单击提交按钮时,会调用函数hello()。但当我单击“立即发送”链接时,会调用函数hello
<script>
function hello()
{
var data="hello";
<a href="www.mytestsite.com?var="' +data +'>Send now</a>
}
</script>
函数hello()
{
var data=“hello”;
}
当用户单击提交按钮时,会调用函数hello()。但当我单击“立即发送”链接时,会调用函数hello()。这是结果www.mytestsite.com?var=
。即数据不会附加到链接中。我在这方面出了什么问题
提前感谢。如果javascript只在单击按钮时执行,并且您希望重定向用户,则不需要锚定标记。只需重定向用户
function hello() {
var data = "foobar";
window.location.href = "http://example.com?data=" + data;
}
试试这个
<script>
function hello()
{
var data="hello";
document.write("<a href='www.mytestsite.com?var=" + data + ">Send now</a>");
}
</script>
函数hello()
{
var data=“hello”;
文件。填写(“”);
}
或者如果您想将带有的链接放入某个html元素中
document.getElementById('DIV').innerHTML = "<a href='www.mytestsite.com?var=" + data + ">Send now</a>";
document.getElementById('DIV').innerHTML=“”;
java脚本代码无效。
应该是这样的:
<script>
function hello()
{
var data="hello";
document.write('<a href="www.mytestsite.com?var='+data+'">Send now</a>');
}
</script>
函数hello()
{
var data=“hello”;
文件。写(“”);
}
只需在锚定标记的属性上添加函数(如hello())
onclick="yourFunction();return false;"
您不能简单地在JavaScript中嵌入锚定标记。我希望在用户单击“立即发送链接”而不是单击按钮时重定向用户。然后在链接上设置事件处理程序,并将链接的href设置为#
。