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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:55:14  来源:igfitidea点击:

How to hide a div with jQuery?

javascriptjquery

提问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 myDivusing .html().

我解决这个问题的方法是获取myDivusing的 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 Contentto the HiddenFieldthen read that contents from the newly created div when I wanted to show the DIV.

然后我把它写到一个新创建的 DIV 中。然后我将 DIV 附加到主体并将变量的内容附加ContentHiddenField然后当我想显示 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...

回答by Nand Kishore Agarwal

$('#myDiv').hide();hide function is used to edit content and show function is used to show again.

$('#myDiv').hide();hide 函数用于编辑内容,show 函数用于再次显示。

For more please click on this link.

如需更多信息,请点击此链接