Javascript 如何使用 jQuery 隐藏 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5375449/
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
How to hide a div with jQuery?
提问by kamaci
When I want to hide a HTML <div>
, I use the following JavaScript code:
当我想隐藏 HTML 时<div>
,我使用以下 JavaScript 代码:
var div = document.getElementById('myDiv');
div.style.visibility = "hidden";
div.style.display = "none";
What is the equivalent of that code in jQuery?
jQuery 中该代码的等价物是什么?
回答by Sujit Agarwal
$('#myDiv').hide();
or
或者
$('#myDiv').slideUp();
or
或者
$('#myDiv').fadeOut();
回答by ctcherry
回答by honk31
$("#myDiv").hide();
will set the css display to none. if you need to set visibility to hidden as well, could do this via
会将 css 显示设置为无。如果您还需要将可见性设置为隐藏,可以通过
$("#myDiv").css("visibility", "hidden");
or combine both in a chain
或将两者结合在一个链中
$("#myDiv").hide().css("visibility", "hidden");
or write everything with one css() function
或用一个 css() 函数编写所有内容
$("#myDiv").css({
display: "none",
visibility: "hidden"
});
回答by specialscope
If you want the element to keep its space then you need to use,
如果您希望元素保持其空间,则需要使用,
$('#myDiv').css('visibility','hidden')
If you dont want the element to retain its space, then you can use,
如果您不希望元素保留其空间,则可以使用,
$('#myDiv').css('display','none')
or simply,
或者干脆,
$('#myDiv').hide();
回答by Cecil Theodore
$("myDiv").hide();
and $("myDiv").show();
does not work in Internet Explorer that well.
$("myDiv").hide();
并且$("myDiv").show();
在 Internet Explorer 中不能很好地工作。
The way I got around this was to get the html content of myDiv
using .html()
.
我解决这个问题的方法是获取myDiv
using的 html 内容 .html()
。
I then wrote it to a newly created DIV. I then appended the DIV to the body and appended the content of the variable Content
to the HiddenField
then read that contents from the newly created div when I wanted to show the DIV.
然后我把它写到一个新创建的 DIV 中。然后我将 DIV 附加到主体并将变量的内容附加Content
到HiddenField
然后当我想显示 DIV 时从新创建的 div 中读取该内容。
After I used the .remove()
method to get rid of the DIV that was temporarily holding my DIVs html.
在我使用该.remove()
方法摆脱临时保存我的 DIV html 的 DIV 之后。
var Content = $('myDiv').html();
$('myDiv').empty();
var hiddenField = $("<input type='hidden' id='myDiv2'>");
$('body').append(hiddenField);
HiddenField.val(Content);
and then when I wanted to SHOW the content again.
然后当我想再次显示内容时。
var Content = $('myDiv');
Content.html($('#myDiv2').val());
$('#myDiv2').remove();
This was more reliable that the .hide()
& .show()
methods.
这比.hide()
&.show()
方法更可靠。
回答by sajoshi
$('#myDiv').hide()
will hide the div...
$('#myDiv').hide()
将隐藏div...