我试图给一个按钮一个onclick事件,当一个页面上的某个东西改变。我试图用很多不同的方式去做,而且都没有成功。我究竟做错了什么?JavaScript动态onClick问题
下面是我试过的。
document.getElementById(subDiv).onclick = function() { alert('Error. There is an error on the the page. Please correct that then submit the page'); };
document.getElementById(subDiv).onclick = "alert('Error. There is an error on the the page. Please correct that then submit the page');";
function redErrorAlert()
{
alert('Error. There is an error on the the page. Please correct that then submit the page');
}
document.getElementById(subDiv).onclick = redErrorAlert;
document.getElementById(subDiv).setAttribute('onclick',redErrorAlert(), false);
document.getElementById(subDiv).setAttribute('onclick','redErrorAlert()', false);
注意:subDiv是一个包含元素id的变量。
你真的应该考虑使用像jquery这样的js库,因为它使得这个al更轻松并且跨浏览器兼容。这就是说.. 什么浏览器你试图在这个测试? – spinon 2010-07-06 21:10:34
@spinon我在Chrome中测试,但它需要在IE,FF和Chrome中工作 – Jason 2010-07-06 21:14:01