我怎样才能用 jquery 看到一个不可见的控件(隐藏和显示不起作用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3025246/
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 can i visible an invisible control with jquery (hide and show not work)
提问by SilverLight
How can I change the visibility of a control with jQuery? I have a control that its visible property to false...(not css)
如何使用 jQuery 更改控件的可见性?我有一个控件,它的可见属性为 false ...(不是 css)
When I used show()
function for it nothing happened, it seems that hide()
and show()
methods are for css set of a control , not visible property...
当我show()
为它使用函数时什么也没发生,似乎hide()
和show()
方法是针对控件的 css 集,而不是可见属性...
Thanks for your answers,
谢谢你的回答,
Best regards
此致
回答by Nick Craver
You can't do this with jQuery, visible="false"
in asp.net means the control isn't rendered into the page. If you want the control to go to the client, you need to do style="display: none;"
so it's actually in the HTML, otherwise there's literally nothing for the client to show, since the element wasn't in the HTML your server sent.
你不能用 jQuery 做到这一点,visible="false"
在 asp.net 中意味着控件不会呈现到页面中。如果您希望控件转到客户端,您需要这样做style="display: none;"
,它实际上在 HTML 中,否则客户端实际上没有任何内容可显示,因为该元素不在您的服务器发送的 HTML 中。
If you remove the visible
attribute and add the style
attribute you can then use jQuery to show it, like this:
如果删除该visible
属性并添加该style
属性,则可以使用 jQuery 来显示它,如下所示:
$("#elementID").show();
Old Answer (before patrick'scatch)
旧答案(在帕特里克抓住之前)
To change visibility
, you need to use .css()
, like this:
要更改visibility
,您需要使用.css()
,如下所示:
$("#elem").css('visibility', 'visible');
Unless you need to have the element occupy page space though, use display: none;
instead of visibility: hidden;
in your CSS, then just do:
除非您需要让元素占用页面空间,否则在您的 CSS 中使用display: none;
而不是visibility: hidden;
,然后执行以下操作:
$("#elem").show();
The .show()
and .hide()
functions deal with display
instead of visibility
, like most of the jQuery functions :)
回答by Harold1983-
.show() and .hide() modify the css display rule. I think you want:
.show() 和 .hide() 修改css显示规则。我想你想要:
$(selector).css('visibility', 'hidden'); // Hide element
$(selector).css('visibility', 'visible'); // Show element
回答by Andy
Here's some code I use to deal with this.
这是我用来处理这个问题的一些代码。
First we show the element, which will typically set the display type to "block" via .show() function, and then set the CSS rule to "visible":
首先我们显示元素,它通常会通过 .show() 函数将显示类型设置为“块”,然后将 CSS 规则设置为“可见”:
jQuery( '.element' ).show().css( 'visibility', 'visible' );
Or, assuming that the class that is hiding the element is called hidden, such as in Twitter Bootstrap, toggleClass() can be useful:
或者,假设隐藏元素的类称为 hidden,例如在 Twitter Bootstrap 中,toggleClass() 可能很有用:
jQuery( '.element' ).toggleClass( 'hidden' );
Lastly, if you want to chain functions, perhaps with fancy with a fading effect, you can do it like so:
最后,如果你想链接函数,也许有淡入淡出的效果,你可以这样做:
jQuery( '.element' ).css( 'visibility', 'visible' ).fadeIn( 5000 );