Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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 如何让onclick转到getCookie函数?_Javascript_Html - Fatal编程技术网

Javascript 如何让onclick转到getCookie函数?

Javascript 如何让onclick转到getCookie函数?,javascript,html,Javascript,Html,我的目标是,当您按下按钮时,它会运行getCookie函数,但为什么它不工作呢 代码如下: <!DOCTYPE html> <html> <body> <button type="button" id="disbtn" onclick="getCookie(usertxt)">Say</button> <h1 id="txt11">hej</h1> <h1 id="txt22">hej2</h1

我的目标是,当您按下按钮时,它会运行getCookie函数,但为什么它不工作呢

代码如下:

<!DOCTYPE html>
<html>
<body>
<button type="button" id="disbtn" onclick="getCookie(usertxt)">Say</button>
<h1 id="txt11">hej</h1>
<h1 id="txt22">hej2</h1>
<script>
function getCookie(usertxt)
{
  var say1 = usertxt;
  document.getElementById("txt11").innerHTML=say1;
  document.getElementById("txt22").innerHTML=say2;
}
</script>
</body>
</html>

说
嘿
hej2
函数getCookie(usertxt)
{
var say1=usertxt;
document.getElementById(“txt11”).innerHTML=say1;
document.getElementById(“txt22”).innerHTML=say2;
}

您必须将usertxt放在引号中

<button type="button" id="disbtn" onclick="getCookie('usertxt')">Say</button>

那就行了

它不起作用,因为“usertxt”变量没有在代码中的任何地方定义。getCookie函数的目标是什么?
您能详细说明一下,以便我们更好地了解您想要做什么吗?

您需要将HTML更新为

<button type="button" id="disbtn" onclick="getCookie(' My text')">Say</button>
<h1 id="txt11">hej</h1>
<h1 id="txt22">hej2</h1>`

我是否必须创建另一个getCookie函数,或者我可以在usertxt函数中创建它?不,您不需要创建另一个函数。您需要更正现有代码中的错误。
function getCookie(usertxt)
{
  var say1 = usertxt;
  var say2 = ' dd dsa '
  document.getElementById("txt11").innerHTML=say1;
  document.getElementById("txt22").innerHTML=say2;
}