这个空字符串是什么意思?
<script type="text/javascript">
var step = 4;
function expandPanel()
{
var panel = document.getElementById('panel');
if ( panel.clientHeight < (panel.originalHeight-step))
{
//clientWidth
var h = panel.clientHeight + step;
panel.style.height = h+"px";
setTimeout("expandPanel()", 100);
}
else
{
panel.style.height = "";
var panelTitle = document.getElementById('panelTitle');
panelTitle.firstChild.nodeValue = 'Collapse';
}
}
function collapsePanel()
{
var panel = document.getElementById('panel');
if ( panel.clientHeight >= step)
{
var h = panel.clientHeight - step;
panel.style.height = h+"px";
setTimeout("collapsePanel()", 100);
}
else
{
panel.style.display = 'none';
var panelTitle = document.getElementById('panelTitle');
panelTitle.firstChild.nodeValue = 'Expand';
}
}
function changePanel()
{
var panel = document.getElementById('panel');
if (!panel.style.height ||
(panel.style.display == 'none'))
{
if (panel.style.display == 'none')
{
panel.style.display = '';
expandPanel();
}
else
{
panel.originalHeight = panel.clientHeight;
collapsePanel();
}
}
}
</script>
为 height
和 display
CSS 属性分配了一个空字符串(通过 CSSOM)。 在这种情况下这意味着什么?
<script type="text/javascript">
var step = 4;
function expandPanel()
{
var panel = document.getElementById('panel');
if ( panel.clientHeight < (panel.originalHeight-step))
{
//clientWidth
var h = panel.clientHeight + step;
panel.style.height = h+"px";
setTimeout("expandPanel()", 100);
}
else
{
panel.style.height = "";
var panelTitle = document.getElementById('panelTitle');
panelTitle.firstChild.nodeValue = 'Collapse';
}
}
function collapsePanel()
{
var panel = document.getElementById('panel');
if ( panel.clientHeight >= step)
{
var h = panel.clientHeight - step;
panel.style.height = h+"px";
setTimeout("collapsePanel()", 100);
}
else
{
panel.style.display = 'none';
var panelTitle = document.getElementById('panelTitle');
panelTitle.firstChild.nodeValue = 'Expand';
}
}
function changePanel()
{
var panel = document.getElementById('panel');
if (!panel.style.height ||
(panel.style.display == 'none'))
{
if (panel.style.display == 'none')
{
panel.style.display = '';
expandPanel();
}
else
{
panel.originalHeight = panel.clientHeight;
collapsePanel();
}
}
}
</script>
There is an empty string assigned to the height
and display
CSS properties (via CSSOM).
What does it mean in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
element.style
属性设置为 ''(空字符串)可让它们采用继承值或默认值。例如,将
element.style.display
设置为空字符串,这是显示之前使用display = 'none';
隐藏的元素的首选方式,这样您就可以不必知道原始属性是什么(它可能根本没有设置)。请注意,在某些浏览器中,更改 DOM 属性将修改相关的 HTML 属性,但在其他浏览器中则不会。此外,如何设置该属性取决于实现。因此,不要依赖这种行为,只需处理属性即可。
在一些浏览器中尝试以下操作:
Firefox 5:背景颜色:红色;边框:1px 纯蓝色;
IE 8: [对象]
Chrome 14: 背景颜色:红色;上边框宽度:1px;右边框宽度:1px;边框底部宽度:1px;左边框宽度:1px;边框顶部样式:实心;右边框样式:实心;边框底部样式:实心;左边框样式:实心;顶部边框颜色:蓝色;右边框颜色:蓝色;边框底部颜色:蓝色;左边框颜色:蓝色;
Opera 11:背景颜色:#ff0000;顶部边框颜色:#0000ff;左边框颜色:#0000ff;右边框颜色:#0000ff;边框底部颜色:#0000ff;上边框宽度:1px;左边框宽度:1px;右边框宽度:1px;边框底部宽度:1px;边框顶部样式:实心;左边框样式:实心;右边框样式:实心;边框底部样式:实心
Setting
element.style
properties to '' (empty string) lets them adopt the inherited or default value.For example, setting
element.style.display
to empty string it is the preferred way to show an element that was previously hidden withdisplay = 'none';
, that way you don't have to know what the original property was (it may not have been set at all).Please note that in some browsers, changing a DOM property will modify the related HTML attribute but in other browsers it won't. Also, how the attribute is set it is implementation dependent. So do not rely on that behaviour, just deal with properties.
Try the following in a few browsers:
Firefox 5: background-color: red; border: 1px solid blue;
IE 8: [object]
Chrome 14: background-color: red; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: blue; border-right-color: blue; border-bottom-color: blue; border-left-color: blue;
Opera 11: background-color: #ff0000; border-top-color: #0000ff; border-left-color: #0000ff; border-right-color: #0000ff; border-bottom-color: #0000ff; border-top-width: 1px; border-left-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-left-style: solid; border-right-style: solid; border-bottom-style: solid
这个例子应该有帮助:
This example should help:
它所做的只是清除 CSS 属性。如果 style 属性以前看起来像这样:
现在它看起来像这样:
All it does is just wipes out that CSS property. If the style attribute looked like this before:
It will now look like this: