如何用jQuery隐藏div ?

时间:2022-12-02 17:46:47

When I want to hide a HTML <div>, I use the following JavaScript code:

当我想隐藏HTML

时,我使用以下JavaScript代码:

var div = document.getElementById('myDiv');
div.style.visibility = "hidden";
div.style.display = "none";

What is the equivalent of that code in jQuery?

jQuery中的代码等价于什么?

6 个解决方案

#1


102  

$('#myDiv').hide();

or

$('#myDiv').slideUp();

or

$('#myDiv').fadeOut();

#2


23  

Easy:

容易:

$('#myDiv').hide();

http://api.jquery.com/hide/

http://api.jquery.com/hide/

#3


18  

$("#myDiv").hide();

will set the css display to none. if you need to set visibility to hidden as well, could do this via

将css显示设置为none。如果您也需要将可视性设置为hidden,可以通过以下方式进行

$("#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"
});

#4


10  

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();

#5


7  

$("myDiv").hide(); and $("myDiv").show(); does not work in Internet Explorer that well.

$(" myDiv ")hide();和$(“myDiv”),告诉();在Internet Explorer中没有那么好用。

The way I got around this was to get the html content of myDiv using .html().

我解决这个问题的方法是使用.html()获取myDiv的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添加到body中,并将变量内容的内容附加到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()方法更可靠。

#6


5  

$('#myDiv').hide() will hide the div...

$('#myDiv').hide()将隐藏div…

#1


102  

$('#myDiv').hide();

or

$('#myDiv').slideUp();

or

$('#myDiv').fadeOut();

#2


23  

Easy:

容易:

$('#myDiv').hide();

http://api.jquery.com/hide/

http://api.jquery.com/hide/

#3


18  

$("#myDiv").hide();

will set the css display to none. if you need to set visibility to hidden as well, could do this via

将css显示设置为none。如果您也需要将可视性设置为hidden,可以通过以下方式进行

$("#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"
});

#4


10  

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();

#5


7  

$("myDiv").hide(); and $("myDiv").show(); does not work in Internet Explorer that well.

$(" myDiv ")hide();和$(“myDiv”),告诉();在Internet Explorer中没有那么好用。

The way I got around this was to get the html content of myDiv using .html().

我解决这个问题的方法是使用.html()获取myDiv的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添加到body中,并将变量内容的内容附加到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()方法更可靠。

#6


5  

$('#myDiv').hide() will hide the div...

$('#myDiv').hide()将隐藏div…