JavaScript 不响应元素点击?

发布于 2024-12-18 13:27:34 字数 271 浏览 0 评论 0原文

这是有问题的代码片段:

       ...     
        var btnClick = document.getElementById("button");
        btnClick .addEventListener("click", func, false);
    }
    function func()
    {
        alert("This works");
    }

我没有收到任何警报框。看到任何问题吗?

This is the piece of code in question:

       ...     
        var btnClick = document.getElementById("button");
        btnClick .addEventListener("click", func, false);
    }
    function func()
    {
        alert("This works");
    }

I don't get any alert box. See any problems?

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

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

发布评论

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

评论(4

贱人配狗天长地久 2024-12-25 13:27:34

您使用哪种浏览器? IE 在版本 9 之前不支持 .addEventListener(),因此您需要这样说:

var btnClick = document.getElementById("button");

if (btnClick.addEventListener){
  btnClick.addEventListener('click', func, false); 
} else if (btnClick.attachEvent){
  btnClick.attachEvent('onclick', func);
}
// and if you're really keen:
else {
  btnClick.onclick = func;
}

并且不要在没有使用 .getElementById() 的元素上使用 .getElementById()尚未被解析,即,将上述代码放在 document.ready 或 load 事件处理程序中,或者将其放在页面源中的元素之后。

Which browser are you using? IE doesn't support .addEventListener() until version 9, so you need to say something like this:

var btnClick = document.getElementById("button");

if (btnClick.addEventListener){
  btnClick.addEventListener('click', func, false); 
} else if (btnClick.attachEvent){
  btnClick.attachEvent('onclick', func);
}
// and if you're really keen:
else {
  btnClick.onclick = func;
}

And don't use .getElementById() on an element that hasn't been parsed yet, i.e., put the above code either in a document.ready or load event handler, or put it after the element in the page source.

分分钟 2024-12-25 13:27:34

这是因为你没有初始化它......!!!

window.onload = prepareButton;

function prepareButton()
{
    document.getElementById('button').onclick = function()
    {
        alert('you clicked me!');
    }
}

Its because of the reason that you have not initialised it....!!!

window.onload = prepareButton;

function prepareButton()
{
    document.getElementById('button').onclick = function()
    {
        alert('you clicked me!');
    }
}
小…楫夜泊 2024-12-25 13:27:34

如果执行以下所有操作,这将起作用:

  1. 在尝试安装事件侦听器之前等待文档加载。您可以在 window.onload 处理程序中运行此代码,也可以将 javascript 初始化代码放在正文的末尾。您可以通过查看 btnClick 是否有效或为空来验证这是否有效。
  2. 仅在支持 addEventListener 的较新浏览器中运行此代码(旧版本的 IE 不支持)。
  3. 确保页面中只有一个带有 id="button" 的元素。
  4. 确保页面中不存在导致脚本停止运行的 JavaScript 错误。
  5. 删除 .addEventListener 之前 btnClick 后面的空格。

您可以在这里看到它的工作原理: http://jsfiddle.net/jfriend00/HQ24Z/

If you do all of the following, this will work:

  1. Wait for the document to load before trying to install the event listener. You can either run this code in a window.onload handler or place the javascript initialization code at the end of the body. You can verify if this is or isn't working by seeing if btnClick is valid or null.
  2. Only run this code in newer browsers that support addEventListener (old versions of IE don't support that).
  3. Make sure there is one and only one element in the page with id="button".
  4. Make sure there are no javascript errors in your page causing your script to stop running.
  5. Remove the space after btnClick before .addEventListener.

You can see it work here: http://jsfiddle.net/jfriend00/HQ24Z/

月下客 2024-12-25 13:27:34

切勿使用“按钮”等非常通用的术语,因为它们往往是保留的,如果您绝对确定它应该有效,您最终会抓狂。使用 id 属性的描述性值,例如“form_4_button_submit”。

还有一个如何进行跨浏览器事件处理的示例......

if (window.addEventListener) {document.addEventListener('keydown',keyPressed,false);}
else {document.attachEvent('onkeydown',keyPressed);}

function keyPressed(evt)
{
 var e = evt || event;
 var key = e.which || e.keyCode;

 switch (key)
 {
  case 77:// M
  alert('m key pressed');
  break;

  case 76://L
  alert('L key pressed');
  break;
 }
}

Never use very generic terms such as 'button' as they tend to be reserved and you'll end up ripping your hair out if you're absolutely sure it should work. Use descriptive values for id attributes such as 'form_4_button_submit'.

Also an example of how to do cross-browser event handling...

if (window.addEventListener) {document.addEventListener('keydown',keyPressed,false);}
else {document.attachEvent('onkeydown',keyPressed);}

function keyPressed(evt)
{
 var e = evt || event;
 var key = e.which || e.keyCode;

 switch (key)
 {
  case 77:// M
  alert('m key pressed');
  break;

  case 76://L
  alert('L key pressed');
  break;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文