click() (javascript) method is not working in FF

前端 未结 3 1624
不知归路
不知归路 2021-01-26 11:02

The following code is throwing two alerts as expected in IE but not in Firefox. Please help.



        
相关标签:
3条回答
  • 2021-01-26 11:37

    Because the <p> tag does not have the method click.

    0 讨论(0)
  • 2021-01-26 11:50

    There's no click method on elements. Are you using any library?

    Usually you have to do something like element.fireEvent('click') (prototype, mootools)

    or element.click() (jquery)

    UPDATE- Similar question: How do I programmatically click on an element in JavaScript?

    Looks like an ugly and brittle solution, if I were you I'd just include jQuery and let that handle all the browser quirks.

    0 讨论(0)
  • 2021-01-26 11:55

    Firefox only has a click() function for form elements such as buttons. However, you can call the onClick function directly; you can change the line to

    document.getElementById('mylabel').onclick();
    

    This works in firefox or IE (but note that it requires that the function actually exists, which you know it does in this example).

    Also note that you aren't actually simulating a click on that element (so, for example, if there were other things that such a click would do, such as also act as a click on the container, they won't happen). You're just getting the function that would run on a click, and running it directly. So it's not a solution for all situations where you need to simulate a click.

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