试图用 javascript 使 div 消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25209834/
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
Trying to make a div disappear with javascript
提问by user3860911
I am trying to make a div disappear with javascript. It just doesn't work. What am I doing wrong?
我正在尝试使用 javascript 使 div 消失。它只是不起作用。我究竟做错了什么?
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
document.getElementById("des").style.visibility = "hidden";
</script>
<div id="des">
Text.
<a href="">link</a>
</div>
回答by Derongan
You are running the script before the DOM has loaded.
您正在 DOM 加载之前运行脚本。
If you put the script after the div, it works
如果你把脚本放在 div 之后,它就可以工作
回答by Evan
Try and throw a document ready around your code.
尝试在您的代码周围准备好文档。
And if you are loading jquery you can just do $('#des').css('visibility', 'hidden');
or $('#des').hide()
如果您正在加载 jquery,您可以执行$('#des').css('visibility', 'hidden');
或$('#des').hide()
<script type="text/javascript">
$(document).ready(function(){
$('#des').css('visibility', 'hidden');
});
</script>
回答by undefined
You are trying to get the element with id = "des", before it's created.
您试图在创建之前获取 id = "des" 的元素。
<div id="des">
Text.
<a href="">link</a>
</div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
document.getElementById("des").style.visibility = "hidden";
</script>
This should work.
这应该有效。
回答by Lal
You should wrap the script in a $(document).ready
block because you are calling the script before the DOM is loaded.
您应该将脚本包装在一个$(document).ready
块中,因为您是在加载 DOM 之前调用脚本。
So,You will have to do it like this
所以,你必须这样做
<script type="text/javascript">
$(document).ready(function() {
document.getElementById("des").style.visibility = "hidden";
});
</script>
回答by Giannis Tzagarakis
this worked for me when I placed the javascript code in a function and load it when the body loads e.g
当我将 javascript 代码放在一个函数中并在身体加载时加载它时,这对我有用,例如
<script>
function func(){
document.getElementById("der").style.visibility = "hidden";
}
</script>
<body onload=func()>
<div id="der">
test
</div>
</body>
回答by Craig Smith
You need to wait for the document to be ready. Try using:
您需要等待文档准备就绪。尝试使用:
$( document ).ready(function() {
document.getElementById("des").style.visibility = "hidden";
});
$( document ).ready(function() {
document.getElementById("des").style.visibility = "hidden";
});
or you could use JQuery:
或者你可以使用 JQuery:
$(documet).ready(function() {
$( ".des" ).hide();
});
$(documet).ready(function() {
$( ".des" ).hide();
});
回答by durbnpoisn
It's:
它的:
document.getElementById("des").style.display = "none";
you can also use:
您还可以使用:
document.getElementById("des").style.display = "block";
to make it visible again.
使其再次可见。
That's my preferred method, at any rate.
无论如何,这是我的首选方法。
回答by Ashish Balchandani
Why not to use JQuery hide()
method, as you already using JQuery and obiviously code to be included either in $(document).ready(function(){\\some code})
or $(window).load(function(){\\some code})
;
为什么不使用 JQueryhide()
方法,因为您已经在使用 JQuery 并且显然代码要包含在$(document).ready(function(){\\some code})
或 中$(window).load(function(){\\some code})
;
$('#des').hide()
in JS, you can achieve by
在JS中,你可以通过
document.getElementById("des").style.display = "none";
回答by Sachin Thapa
Javascript is an interepreter based language, in case you want to write script first and use later, add a function instead.
Javascript 是一种基于解释器的语言,如果您想先编写脚本并稍后使用,请添加一个函数。
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function hideMyDiv(){
document.getElementById("des").style.visibility = "hidden";
}
</script>
<div id="des">
Text.
<a href="">link</a>
</div>
<script type="text/javascript">
hideMyDiv();
</script>
Cheers !!
干杯!!