jQuery 删除属性显示:无;因此该项目将可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3654068/
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
remove attribute display:none; so the item will be visible
提问by mt0s
The element is:
元素是:
span {
position:absolute;
float:left;
height:80px;
width:150px;
top:210px;
left:320px;
background-color:yellow;
display:none; //No display
border: 3px solid #111;
}
I use this code to remove the display so it can be visible,
我使用此代码删除显示,以便它可以看到,
$("span").removeAttr("display");
$("span").removeAttr("display");
but it does not work. Is the method I'm using valid or is there an other way to get the result?
但它不起作用。我使用的方法有效还是有其他方法可以获得结果?
回答by Nikita Rybak
For this particular purpose, $("span").show()
should be good enough.
对于这个特定的目的,$("span").show()
应该足够好了。
回答by meder omuraliev
$('#lol').get(0).style.display=''
$('#lol').get(0).style.display=''
or..
或者..
$('#lol').css('display', '')
回答by BalusC
The removeAttr()
function only removes HTML attributes. The display
is not a HTML attribute, it's a CSS property. You'd like to use css()
function instead to manage CSS properties.
该removeAttr()
函数仅删除 HTML 属性。该display
不是HTML属性,这是一个CSS属性。您想使用css()
函数来管理 CSS 属性。
But jQuery offers a show()
function which does exactly what you want in a concise call:
但是 jQuery 提供了一个show()
函数,它可以在简洁的调用中完成您想要的功能:
$("span").show();
回答by Beedjees
You should remove "style" attribute instead of "display" property :
您应该删除“样式”属性而不是“显示”属性:
$("span").removeAttr("style");
回答by J-D
If you are planning to hide show some span based on click event which is initially hidden with style="display:none" then .toggle() is best option to go with.
如果您打算根据最初使用 style="display:none" 隐藏的点击事件隐藏显示一些跨度,那么 .toggle() 是最好的选择。
$("span").toggle();
$("span").toggle();
Reasons :Each time you don't need to check whether the style is already there or not. .toggle() will take care of that automatically and hide/show span based on current state.
原因:每次不需要检查样式是否已经存在。.toggle() 将自动处理并根据当前状态隐藏/显示跨度。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Toggle" onclick="$('#hiddenSpan').toggle();"/>
<br/>
<br/>
<span id="hiddenSpan" style="display:none">Just toggle me</span>
回答by mkoistinen
The jQuery you're using is manipulates the DOM, not the CSS itself. Try changing the word span
in your CSS to .mySpan
, then apply that class to one or more DOM elements in your HTML like so:
您使用的 jQuery 是操作 DOM,而不是 CSS 本身。尝试将span
CSS 中的单词更改为.mySpan
,然后将该类应用于 HTML 中的一个或多个 DOM 元素,如下所示:
...
<span class="mySpan">...</span>
...
Then, change your jQuery as follows:
然后,按如下方式更改您的 jQuery:
$(".mySpan").css({ display : inline });
This should work much better.
这应该工作得更好。
Good luck!
祝你好运!