Javascript onclick函数不起作用

编程入门 行业动态 更新时间:2024-10-24 22:21:06
本文介绍了Javascript onclick函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script language="javascript" type="text/javascript"> function setFont() { var i; for ( i = 0; i < document.all.length; i++) { document.all[i].style.fontFamily = "Verdana"; document.all[i].style.fontSize = "16"; document.all[i].style.color="black"; } }; function abc(a) { alert(a); ansArray = ['a']; for (i = 1; i <= a; i++) { document.write('<input type = "button" value = "a">'); document.write('<input type = "button" value = "b">'); } var myButton = document.getElementsByTagName("input"); //alert(myButton.length); myButton[0].onclick = function() { if (ansArray[0] == 'a') myButton[0].style.backgroundColor = "green"; else myButton[0].style.backgroundColor = "red"; }; myButton[1].onclick = function() { if (ansArray[0] == 'b') myButton[1].style.backgroundColor = "green"; else myButton[1].style.backgroundColor = "red"; }; };​ setFont(); </script> </head> <body onload="Javascript:abc(2)"> hello </body> </html>

onclick函数在IE中不起作用,但在chrome和firefox中工作正常。我找不到错误。为什么正常功能不起作用。函数加载内容但是点击事件处理程序所写的前两个按钮不会改变IE中的按钮颜色。请帮帮我...提前致谢

The onclick functions do not work in IE but work fine in chrome and firefox. I could not find the mistake. Why a normal function does not work. function loads the contents but onclicking the first two buttons for which event handelers are writen does not change the button colour in IE only. Please help me... Thanks in advance

推荐答案

快速谷歌搜索表明问题是你在使用document.write之后页面已被加载,所以它也正在删除dom。你应该避免在页面加载后调用的函数中使用它。

quick googling suggests that the problem is that you are using document.write after the page has been loaded so it is erasing the dom as well. you should avoid using that in functions that are called after page loading.

来源: sitr.us/2012/09/04/monkey-patching-document-write.html

我没有IE所以不能不测试它。

I dont have IE so couldn't test it.

更多推荐

Javascript onclick函数不起作用

本文发布于:2023-11-27 21:17:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   不起作用   Javascript   onclick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!