Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过href链接发送JS变量_Javascript_Html_Href - Fatal编程技术网

Javascript 通过href链接发送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

我正在尝试向特定网页发送一个JS变量。下面是我的代码

<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设置为
#