在 javascript 中捕获事件

发布于 2024-12-29 07:31:49 字数 438 浏览 3 评论 0原文

<!doctype html>
<html>

<body>
<div id = 'div' style = 'width:100px;height:100px;background:#000000;'></div>
<script type = 'text/javascript'>
document.getElementById('div').addEventListener('click',happen(),true);
function happen()
{
    alert(1)
}
</script>
</body>
</html>

在上面的代码中,为什么当页面加载时会触发该事件,而当我单击 div 时不会触发该事件...另外,正确的事件名称是 click 或 onclick...

<!doctype html>
<html>

<body>
<div id = 'div' style = 'width:100px;height:100px;background:#000000;'></div>
<script type = 'text/javascript'>
document.getElementById('div').addEventListener('click',happen(),true);
function happen()
{
    alert(1)
}
</script>
</body>
</html>

In the above code why the event is triggered when the page loads and not triggered when i click on the div...Also which is the correct event name click or onclick....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

溺ぐ爱和你が 2025-01-05 07:31:49

这是因为您立即调用了该函数,并将其 null 结果传递给 addEventListener()

它应该是:

document.getElementById('div').addEventListener('click',happen,true);

如果你想将参数传递给 happen,你必须这样写:

document.getElementById('div').addEventListener('click', function() {
    happen(args_here, ...); 
}, true);

It's because you've immediately called the function, and passed its null result to addEventListener().

It should be:

document.getElementById('div').addEventListener('click',happen,true);

If you want to pass arguments to happen, you'd have to write this:

document.getElementById('div').addEventListener('click', function() {
    happen(args_here, ...); 
}, true);
酒与心事 2025-01-05 07:31:49

立即调用该函数并将其返回值传递给addEventListener,就像任何其他函数调用一样。
取出()

You're calling the function immediately and passing its return value to addEventListener, just like any other function call.
Take out the ().

近箐 2025-01-05 07:31:49

这应该有效:

document.getElementById('div').addEventListener('click',happen,true);

This should work:

document.getElementById('div').addEventListener('click',happen,true);
橘香 2025-01-05 07:31:49

问题出在这一行:

document.getElementById('div').addEventListener('click',happen(),true);

您应该只传递函数 happen 的名称,但由于添加了括号,所以您正在传递结果。

试试这个:

document.getElementById('div').addEventListener('click',happen,true);

The problem is with this line:

document.getElementById('div').addEventListener('click',happen(),true);

You should should only be passing the name of the function happen but since you added the parentheses, you are passing the result.

Try this instead:

document.getElementById('div').addEventListener('click',happen,true);
烛影斜 2025-01-05 07:31:49

正如其他回答者所说,去掉()将解决问题。

另一种方法是将函数包装在 function(){ } 中,这对于 OO Javascript 来说是一个很好的实践,如下所示:

document.getElementById('div').addEventListener('click ',function(){happen()},true);

如果回调需要在对象内执行(在本例中不需要),这将保留范围。

As the other answerers have said, taking out the () will fix the problem.

An alternative, and a good practice for OO Javascript, is to wrap the function inside a function(){ }, like so:

document.getElementById('div').addEventListener('click',function(){happen()},true);

That will retain scope if the callback needs to execute within an object (in this case it does not).

雨落□心尘 2025-01-05 07:31:49

事件处理程序不需要括号

 document.getElementById('div1').addEventListener('click',happen,true);

The event handler does not need parenthesis

 document.getElementById('div1').addEventListener('click',happen,true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文