问题
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:
JavaScript is enabled in your browser.
Try adding a
return false;statement in your tag.
Or a function like this:
function test() {
//your code here
return false;
}
- 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