Javascript 检查 hidden 是否等于 true 或 false jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10008312/
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 23:28:17  来源:igfitidea点击:

check if hidden equals true or false jQuery

javascriptjqueryget

提问by p0rter

i've got this

我有这个

$('#div').attr("hidden", true);

i tried:

我试过:

var a = $('#div').attr("hidden");
var b = $('#div').attr("hidden").val();
var c = $('#div').hidden;
var a = $('#div').disabled;

i just want to know whether hidden is true or false. does anybody know? my research results are all about forms and inputs.

我只想知道隐藏是真的还是假的。有人知道吗?我的研究结果都是关于形式和投入的。

回答by gdoron is supporting Monica

attribute will never be true, it can have strings only.
jQuery has the datafunctions for objects other than strings:

属性永远不会是true,它只能有字符串。
jQuery 具有data用于字符串以外的对象的函数:

$('#div').data("hidden", true);      // set the "hidden" data
var flag = $('#div').data("hidden"); // get the "hidden" data (true)

If you wanted to hide the div, use .hide():

如果您想隐藏div,请使用.hide()

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

And you check if the div is visible with :visible\ :hidden

然后您检查 div 是否可见:visible\:hidden

$('#div').is(':visible'); // Or $('#div').is(':hidden')

回答by Haim Evgi

i think you mean jquery visible

我想你的意思是 jquery 可见

.is(':visible')

回答by Daniel Rivers

Alternatively you could use

或者你可以使用

$('#div').toggle(showOrHide);

where showOrHide is a bool of true of false to hide or show.

其中 showOrHide 是隐藏或显示的真假布尔值。

This is the same as doing

这和做一样

if ( showOrHide == true ) {
  $('#div').show();
} else if ( showOrHide == false ) {
  $('#div').hide();
}

Hope this helps

希望这可以帮助