Why is my onclick event not registered in Firefox?

守給你的承諾、 提交于 2020-01-11 08:50:31

问题


I have a list item with an onclick event. It runs in Chrome and Internet Explorer, but not Firefox. Any suggestions?

<li onclick="alert('test');">test test<br></li>

回答1:


This works fine for me in Firefox.

Check this:

  1. JavaScript is enabled in your browser.

  2. Try adding a return false; statement in your tag.

Or a function like this:

function test() {
    //your code here
    return false;
}
  1. Or use this:

<a href="#" onclick="alert('hi');">Link</a>

or this

<a href="javascript:void(0)" onclick="alert('hi');">Link</a>




回答2:


I was trying to minimize my html code to send a complete code to simulate the error as Boris Zbarsky requested. Then I found my mistake.

I was using marquee tag which has been deprecated. Now I am going to use jQuery instead of it.

thx




回答3:


In Firefox, the event object is not global. You have to access it within your script tags not in html.

onclick works likes this

<li id="alert">test<br></li>

<script>
  document.getElementById("alert").addEventListener("click", function( event ) {
    alert('test');
  }, false);
</script>



回答4:


Attributes can be ignored by Firefox when it is served invalid HTML, use https://validator.w3.org/ to clean up the HTML.



来源:https://stackoverflow.com/questions/10852165/why-is-my-onclick-event-not-registered-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!