jQuery 可见性属性问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2690865/
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
Visibility attribute question
提问by Misha Moroshko
What is the difference between
之间有什么区别
$("#myid").attr("style", "visibility: hidden")
$("#myid").attr("style", "visibility: hidden")
and
和
$("#myid").css("visibility", "hidden")
$("#myid").css("visibility", "hidden")
?
?
回答by Nick Craver
Doing this:
这样做:
$("#myid").attr("style", "visibility: hidden")
Will leave onlythis style attribute, while doing this:
将仅保留此样式属性,同时执行以下操作:
$("#myid").css("visibility", "hidden")
Will add(or set) this style attribute.
将添加(或设置)此样式属性。
Here's an example, the first will alwaysresult in this:
这是一个例子,第一个总是会导致这个:
style="visibility: hidden;"
The second just adds visibility
so your style may now be:
第二个只是添加visibility
所以你的风格现在可能是:
style="width: 50px; color: red; visibility: hidden;"
回答by Justin Niessner
Nothing. Just two ways to accomplish the same goal.
没有。只有两种方法可以实现相同的目标。
The first will overwrite any existing style settings. If you had:
第一个将覆盖任何现有的样式设置。如果你有:
<div style="font-weight: bold;" />
It would become:
它会变成:
<div sytle="visibility: hidden;" />
The second will add the visibility setting to the existing styles. So:
第二个将向现有样式添加可见性设置。所以:
<div style="font-weight: bold;" />
Woudl become:
会变成:
<div style="font-weight: bold; visibility: hidden;" />
If there's no style attribute already set, then the two will produce the same end result.
如果尚未设置样式属性,则两者将产生相同的最终结果。
回答by Josh Wright
There isn't really any difference.$.css() is just a shortcut method for accessing the css style attribute of a dom element.
真的没有什么区别。$.css() 只是访问 dom 元素的 css 样式属性的快捷方法。
EDIT: As justin pointed out, there isa difference in that the .attr() method will overwrite any existing style attributes.
编辑:贾斯汀指出的那样,是在该.attr()方法将覆盖所有现有的样式属性的差异。