[英]Javascript not working in browser? (Firefox, Chrome and Safari)
http://pastebin.com/K2miHiAj http://pastebin.com/K2miHiAj
That's where the code is located ^ 这就是代码所在的位置^
I use both Coda 2 and BBEdit to run the code on and test it, and it works perfectly fine on them! 我使用Coda 2和BBEdit来运行代码并对其进行测试,它在它们上运行得非常好! However, whenever I try a browser, the code never runs properly. 但是,每当我尝试使用浏览器时,代码都无法正常运行。 For this assignment the code has to work on Firefox. 对于此分配,代码必须适用于Firefox。
http://oi47.tinypic.com/2lk9yx4.jpg http://oi47.tinypic.com/2lk9yx4.jpg
You've defined the function myFunction
AFTER trying to attach it to the onclick
, therefore the thing in onclick="myFunction()"
is undefined . 你试图将它附加到onclick
后定义了函数myFunction
,因此onclick="myFunction()"
是未定义的 。
Solution: seperate your JavaScript from your HTML. 解决方案:从HTML中分离JavaScript。
// attach listener to window.load
window.addEventListener('load',function () {
// attach listener to your button
document.getElementById('clickme')
.addEventListener('click',
myFunction,
false);
}, false);
Look at this fiddle . 看看这个小提琴 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.