为什么我的 Javascript 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8290710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my Javascript not working?
提问by Ryan Schafer
This is my Javascript:
这是我的Javascript:
<script type="text/javascript">
function contactChange() {
var contact = document.getElementbyId("contact");
if (contact.style.display == "none") {
contact.style.display = "block";
} else {
contact.style.display = "none";
}
}
</script>
And here is my site:
这是我的网站:
回答by JiminP
It's document.getElementById
, not document.getElementbyId
. (In JS, name of variables and functions are case-sensitive)
是document.getElementById
,不是document.getElementbyId
。(在JS中,变量名和函数名是区分大小写的)
Debugging tip : Look at the JS console (F12 in Google Chrome and IE9, Ctrl+Shift+K in Firefox). In this case, following error can be seen:
调试提示:查看 JS 控制台(Google Chrome 和 IE9 中的 F12,Firefox 中的 Ctrl+Shift+K)。在这种情况下,可以看到以下错误:
It shows where the error happened (line 260 in your HTML/JS code) and what the error is(Object #<HTMLDocument>
has no method getElementbyId
).
它显示了错误发生的位置(HTML/JS 代码中的第 260 行)以及错误是什么(对象#<HTMLDocument>
没有方法getElementbyId
)。
回答by Cito
It's getElementById
, not getElementbyId
. Note the upper case "B".
是getElementById
,不是getElementbyId
。注意大写“B”。
回答by chess007
You're going to hate yourself for this, but you put getElementbyId()
instead of getElementById()
. Note the capitalized "B" in the second version.
你会恨自己这一点,但你把getElementbyId()
代替getElementById()
。注意第二个版本中大写的“B”。
回答by Amaan Iqbal
Its getElementById
in place of getElementbyId
它getElementById
代替了 getElementbyId
回答by Ritz
Try to hide then show a element on scroll I had to go this route. Basically i tried to use window so I used 'body'
尝试隐藏然后在滚动上显示一个元素我不得不走这条路。基本上我尝试使用 window 所以我使用了“body”
$(document).ready(function() {
$('body').scroll(function() {
var scroll = $('body').scrollTop();
if (scroll <= 50 ) {
console.log(scroll);
we
} else {
$("#label").css('fill', 'none');
$(".label").addClass(".transition");
}
if (scroll <= 150) {
$(".sizeLG").css('color', '#ffffff');
} else {
$(".sizeLG").css('color', '#00000000');
$(".sizeLG").addClass(".transition");
}
});
});
});