Firefox not registering onclick event handler
Below are two snips of code for assigning an event handler for an onclick event. Version One works in IE, FF, Safari and Chrome. Version Two works in IE, Safari and Chrome, but not in FF. In Version One, I register the event handler in the markup. In Version Two, I use a newer, and supposedly more robust, method for registering the event handler.
Using firebug, it looks like the onclick event handler is not getting registered, though I cannot see why not. Any ideas appreciated. Thanks.
*************** Version One ****************************** < script type="text/javascript"> function handler() { // do something here } </script> < a id="playerFive" class="player" onclick="handler()"> < img src="./images/speakerIcon25pxFFF4E0.png" alt=""/> </a> ************************************************************ *************** Version Two ****************************** < script type="text/javascript"> function handler() { // do something here } </script> < a id="playerFive" class="player"> < img src="./images/speakerIcon25pxFFF4E0.png" alt=""/> </a> < script type="text/javascript"> playerFive.onclick = handler; < /script> ************************************************************
FF does not allow you to access DOM elements as javascript variables named by their ID.
You need to use getElementById
instead.
在上一个脚本块中, playerFive
从未在任何地方定义过,您可能需要
document.getElementById('playerFive').onclick=handler;
你可以试试这个代码
if(document.all)// For IE
{
document.getElementById('playerFive').attachEvent('onclick',handler);
}
else // For FF
{
document.getElementById('playerFive').addEventListener('click',handler,false);
}
链接地址: http://www.djcxy.com/p/88930.html
上一篇: 占位符填充在Firefox中不起作用