Why is my onclick event not registered in Firefox?

后端 未结 4 1785
南方客
南方客 2021-01-18 21:08

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

  • 相关标签:
    4条回答
    • 2021-01-18 21:38

      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>
      
      0 讨论(0)
    • 2021-01-18 21:39

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

      0 讨论(0)
    • 2021-01-18 21:41

      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>

      0 讨论(0)
    • 2021-01-18 21:51

      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

      0 讨论(0)
    提交回复
    热议问题