[英]All Javascript suddenly stopped working
I had two very simple Javascripts on a page, and then when I came back to the site, they are suddenly unresponsive. 我在页面上有两个非常简单的Javascript,然后当我回到站点时,它们突然没有响应。 They do nothing, and no errors show up in the browser console.
它们什么也不做,并且在浏览器控制台中不会显示任何错误。 Its very confusing.
它非常令人困惑。
<div id="menuBtn">
<img src="img/gear.png" onmouseover="showMenu();" onmouseout="hideMenu();" height=30px width=30px>
<script type="text/javascript">
function hideMenu() {
document.getElementById("myMenu").style.visibility = "hidden";
}
function showMenu() {
document.getElementById("myMenu").style.visibility = "visible";
}
</script>
</div>
<input type="checkbox" id="autosave" width=25px onChange="autoSave();">
<img src="img/autosave_disabled.gif" height=25 id="autoimg">
<font id="autosave_label" color="grey">Enable Auto-save</font>
<script type="text/javascript">
function autoSave() {
if(this.checked) {
document.getElementById("autoimg").src = "img/autosave.gif";
document.getElementById("autosave_label").style.color = 'black';
} else {
document.getElementById("autoimg").src = "img/autosave_disabled.gif";
document.getElementById("autosave_label").style.color = 'grey';
}
}
</script>
In my IDE, I am seeing "missing semicolon" errors, but it was working yesterday with no errors, so this doesn't make a lot of sense to me. 在我的IDE中,我看到“缺少分号”错误,但是昨天它没有错误,所以对我来说没有太大意义。
Any assistance you could offer would be appreciated. 您能提供的任何帮助将不胜感激。
The this.checked
in "autoSave" function returned "undefined" for me. 在“ autoSave”函数中的
this.checked
为我返回了“未定义”。 I have modified the code to pass the checkbox element reference to the autoSave function. 我修改了代码,以将复选框元素引用传递给autoSave函数。
<input type="checkbox" id="autosave" width=25px onchange="autoSave(this)">
<img src="img/autosave_disabled.gif" height=25 id="autoimg">
<font id="autosave_label" color="grey">Enable Auto-save</font>
<script type="text/javascript">
function autoSave(element) {
if(element.checked) {
document.getElementById("autoimg").src = "img/autosave.gif";
document.getElementById("autosave_label").style.color = 'black';
} else {
document.getElementById("autoimg").src = "img/autosave_disabled.gif";
document.getElementById("autosave_label").style.color = 'grey';
}
}
</script>
Try this approach (set handlers through addEventListener
): 尝试这种方法(通过
addEventListener
设置处理程序):
var img = document.getElementById("image"), input = document.getElementById("autosave"); img.addEventListener('mouseover', showMenu, false); img.addEventListener('mouseout', hideMenu, false); input.addEventListener('change', autoSave, false); function hideMenu() { console.log('hideMenu'); } function showMenu() { console.log('showMenu'); } function autoSave() { if(this.checked) { console.log('actions if checked'); } else { console.log('actions if not checked'); } }
<div id="menuBtn"> <img id="image" src="http://www.dailyworldfacts.com/wp-content/uploads/2011/06/facts-about-cat-fallen-cat.jpg" height="70px" width="70px"> </div> <input type="checkbox" id="autosave" width=25px > <img src="img/autosave_disabled.gif" height=25 id="autoimg"> <font id="autosave_label" color="grey">Enable Auto-save</font>
Alternatively, you could take a look at jQuery library that offers great tools to accomplish tasks like yours and you don't have to worry that much about cross-browserness. 另外,您可以看一下jQuery库,它提供了出色的工具来完成像您这样的任务,而您不必担心跨浏览器问题。
You don't need the semicolon in an html tag. 您不需要在html标签中使用分号。 I don't know if thats the problem, but its a possible solution.
我不知道这是否是问题,但可能的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.