Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.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事件不工作_Javascript_Events - Fatal编程技术网

Javascript onclick事件不工作

Javascript onclick事件不工作,javascript,events,Javascript,Events,在运行下面的代码时,出现以下错误-“UncaughtTypeError:无法将属性'onclick'设置为null” document.getElementById(“x”).onclick=function(){ 警报(“点击按钮”); }; 这是按钮 您需要设置一个事件侦听器: var button = document.getElementById('x'); button.addEventListener('click', function() { console.log('

在运行下面的代码时,出现以下错误-“UncaughtTypeError:无法将属性'onclick'设置为null”


document.getElementById(“x”).onclick=function(){
警报(“点击按钮”);
};
这是按钮

您需要设置一个事件侦听器:

var button = document.getElementById('x');

button.addEventListener('click', function() {
    console.log('button clicked');    
});

您需要设置一个事件侦听器:

var button = document.getElementById('x');

button.addEventListener('click', function() {
    console.log('button clicked');    
});

您需要设置一个事件侦听器:

var button = document.getElementById('x');

button.addEventListener('click', function() {
    console.log('button clicked');    
});

您需要设置一个事件侦听器:

var button = document.getElementById('x');

button.addEventListener('click', function() {
    console.log('button clicked');    
});

您需要在
之后迁移
元素。引发此错误的原因是
getElementById
返回
null
,因为它找不到元素
#x
,因为此时尚未到达
按钮#x
元素

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>
    <button id="x">this is button</button>
    <script>
       document.getElementById("x").onclick = function() {
           alert("clicked the button");
       };
    </script>
</body>
</html>

这是按钮
document.getElementById(“x”).onclick=function(){
警报(“点击按钮”);
};

您需要在
之后迁移
元素。引发此错误的原因是
getElementById
返回
null
,因为它找不到元素
#x
,因为此时尚未到达
按钮#x
元素

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>
    <button id="x">this is button</button>
    <script>
       document.getElementById("x").onclick = function() {
           alert("clicked the button");
       };
    </script>
</body>
</html>

这是按钮
document.getElementById(“x”).onclick=function(){
警报(“点击按钮”);
};

您需要在
之后迁移
元素。引发此错误的原因是
getElementById
返回
null
,因为它找不到元素
#x
,因为此时尚未到达
按钮#x
元素

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>
    <button id="x">this is button</button>
    <script>
       document.getElementById("x").onclick = function() {
           alert("clicked the button");
       };
    </script>
</body>
</html>

这是按钮
document.getElementById(“x”).onclick=function(){
警报(“点击按钮”);
};

您需要在
之后迁移
元素。引发此错误的原因是
getElementById
返回
null
,因为它找不到元素
#x
,因为此时尚未到达
按钮#x
元素

<!DOCTYPE html>
<html>
    <head>
    </head>
<body>
    <button id="x">this is button</button>
    <script>
       document.getElementById("x").onclick = function() {
           alert("clicked the button");
       };
    </script>
</body>
</html>

这是按钮
document.getElementById(“x”).onclick=function(){
警报(“点击按钮”);
};

无法按id获取元素,因为文档未加载

尝试添加onload函数:

window.onload = function() {
  document.getElementById("x").onclick = function() {
    alert("clicked the button");
  };
};

无法按id获取元素,因为未加载文档

尝试添加onload函数:

window.onload = function() {
  document.getElementById("x").onclick = function() {
    alert("clicked the button");
  };
};

无法按id获取元素,因为未加载文档

尝试添加onload函数:

window.onload = function() {
  document.getElementById("x").onclick = function() {
    alert("clicked the button");
  };
};

无法按id获取元素,因为未加载文档

尝试添加onload函数:

window.onload = function() {
  document.getElementById("x").onclick = function() {
    alert("clicked the button");
  };
};

您试图在DOM中存在按钮之前绑定click事件。将click事件绑定移动到身体底部,或按如下方式更改:

这是按钮

干杯,
Autobox

您试图在DOM中存在按钮之前绑定click事件。将click事件绑定移动到身体底部,或按如下方式更改:

这是按钮

干杯,
Autobox

您试图在DOM中存在按钮之前绑定click事件。将click事件绑定移动到身体底部,或按如下方式更改:

这是按钮

干杯,
Autobox

您试图在DOM中存在按钮之前绑定click事件。将click事件绑定移动到身体底部,或按如下方式更改:

这是按钮

干杯,

AutoBox

Uncaught TypeError:无法读取要引发的错误为null的属性'addEventListener'。我正在w3school editor中运行上述代码。。。杜普。。。事实上,您试图在DOM准备就绪之前到达它。将脚本移出头部并移到bodyUncaught TypeError的末尾:无法读取属性'addEventListener',引发的错误为null。我正在w3school editor中运行上述代码。。。杜普。。。事实上,您试图在DOM准备就绪之前到达它。将脚本移出头部并移到bodyUncaught TypeError的末尾:无法读取属性'addEventListener',引发的错误为null。我正在w3school editor中运行上述代码。。。杜普。。。事实上,您试图在DOM准备就绪之前到达它。将脚本移出头部并移到bodyUncaught TypeError的末尾:无法读取属性'addEventListener',引发的错误为null。我正在w3school editor中运行上述代码。。。杜普。。。事实上,您试图在DOM准备就绪之前到达它。将您的脚本从头部移动到身体的末端开始工作!我会马上接受你的回答。请解释一下这有什么不同。我的看起来不错,对吧?因为在获取元素时dom还没有准备好加载,所以无法在脚本中获取按钮dom。正确的方法是在dom之后写脚本标记,或者在window.onload中写get元素方法。@kenticny我想你是在回复OP,在这种情况下,请使用
@
提及,因为我收到通知:)它工作了!我会马上接受你的回答。请解释一下这有什么不同。我的看起来不错,对吧?因为在获取元素时dom还没有准备好加载,所以无法在脚本中获取按钮dom。正确的方法是在dom之后写脚本标记,或者在window.onload中写get元素方法。@kenticny我想你是在回复OP,在这种情况下,请使用
@
提及,因为我收到通知:)它工作了!我会马上接受你的回答。请解释一下这有什么不同。我的看起来不错,对吧?因为在获取元素时dom还没有准备好加载,所以无法在脚本中获取按钮dom。正确的方法是在dom之后写脚本标记,或者在window.onload中写get元素方法。@kenticny我想你是的