Javascript 使用 .prop(hidden: true) 在 JQuery 中隐藏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14900138/
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
Hiding button in JQuery using .prop(hidden: true)
提问by 2scottish
I am trying to figure out how to hide a button with JQuery using the .prop(hidden: true) method. For some reason, in Chrome when I set this value and view the html, the button has a hidden element, but the button still shows up as visible on the page.
我想弄清楚如何使用 .prop(hidden: true) 方法使用 JQuery 隐藏按钮。出于某种原因,在 Chrome 中,当我设置此值并查看 html 时,该按钮有一个隐藏元素,但该按钮仍然显示为页面上可见。
Any ideas?
有任何想法吗?
回答by adeneo
A button does'nt have a hidden property ?
按钮没有隐藏属性?
$('button').hide();
or
或者
$('button').toggle(true); //shows button
$('button').toggle(false); //hides button
回答by Alexander
You can use set the displaystyle to none. For example,
您可以使用将display样式设置为none. 例如,
$("#button").css("display", "none");
Or, .hide()for brevity,
或者,.hide()为简洁起见,
$("#button").hide()
There's also visibilityand opacitybut these two may not generate the effect you desired.
还有visibility和opacity但是这两个可能不会产生你想要的效果。
回答by Jai
You can't hide a button using jQuery's .prop()function, you have to use either .hide() or .fadeOut()or you can try with .css()method:
你不能使用 jQuery 的.prop()函数隐藏按钮,你必须使用.hide() or .fadeOut()或者你可以尝试使用.css()方法:
using .css():
使用.css():
$('input[submit]').css('display','none');
using fadeOut():
使用fadeOut():
$('input[submit]').fadeOut();
using .hide():
使用.hide():
$('input[submit]').hide();
回答by Pointy
Your syntax is incorrect, but there's no "hidden" property anyway. You probably want:
您的语法不正确,但无论如何都没有“隐藏”属性。你可能想要:
$('#your_button').hide();
or possibly
或者可能
$('#your_button').addClass('hidden');
if you've got a "hidden" class in your CSS.
如果您的 CSS 中有一个“隐藏”类。
The incorrect part of your syntax is that the parameters to your function call are expressed incorrectly. Setting a property should look like:
语法的错误部分是函数调用的参数表达不正确。设置属性应如下所示:
$("#your_button").prop("name", "value");
回答by Robert K
jQuery.prop is intended for HTML attributes only, things defined on the DOM node. CSS styles aren't applicable things to set with prop, and hiddenjust doesn't exist, whereas hrefor classis applicable. Instead you must use $(el).css('display', 'none')or $(el).hide().
jQuery.prop仅用于 HTML 属性,即在 DOM 节点上定义的内容。CSS 样式不适用于用 prop 设置的东西,hidden只是不存在,而href或class是适用的。相反,您必须使用$(el).css('display', 'none')或$(el).hide()。
回答by Curious Sam
What you described is actually correct if you happen to use jquery alongside bootstrap4.
如果您碰巧将 jquery 与 bootstrap4 一起使用,那么您所描述的实际上是正确的。
just do the following:
$element.prop('hidden', true);
只需执行以下操作:
$element.prop('hidden', true);
If no bootstrap 4 available it is still works for modern browser.
如果没有可用的引导程序 4,它仍然适用于现代浏览器。
回答by NuclearPeon
You can use a ternaryoperator and the css()method to accomplish the same thing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
您可以使用ternary运算符和css()方法来完成相同的事情:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
$("#button").css("display", (mycondition) ? "block" : "none");
回答by allenhwkim
If you want to use prop, then
如果你想使用道具,那么
$("#my_button").prop("style").display="none"
I would go w/o jquery. (back to the basic)
我会去没有jquery。(回到基本)
document.getElementById("mybutton").style.display = "none";
回答by darshanags
prop() is a getter function: http://api.jquery.com/prop/I suggest using hide: http://api.jquery.com/hide/
prop() 是一个 getter 函数:http: //api.jquery.com/prop/我建议使用隐藏:http: //api.jquery.com/hide/

