JavaScript 显示=“块”;
我有一些代码,当单击某些内容时会显示表格行。因此,该行的样式属性被禁用,如下所示:
HTML
<tr id='Asset' class='rrtr' style='display:none;'>
用户单击并触发 Javascript,效果很好:
Javascript
document.getElementById("Asset").style.display = 'block';
但是,该行的样式是' t 与其余部分一致,即使它的类属性像其他部分一样设置为“rrtr”。
如果我关闭“显示:无;”放在行上并运行它,显示格式正确。
有什么想法吗?
I have some code that shows a tables row when something is clicked. So, the row has it's style attribute disabled, see below:
HTML
<tr id='Asset' class='rrtr' style='display:none;'>
The user clicks and fires the Javascript, which works fine:
Javascript
document.getElementById("Asset").style.display = 'block';
However, the style of the row isn't in line with rest even though it's class attributes are set to 'rrtr' like the rest.
If I turn off 'display:none;' on the row and run it showing, the format is fine.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了获得最佳兼容性,请将
Internet Explorer 7 及更低版本设置为不支持
table-row
作为display
的值。或者,可以说,更好的想法是为行设置一个类,并使用 JS 删除/更改它:For best compatibility, set
Internet Explorer 7 and lower do not support
table-row
as a value fordisplay
. Alternatively– and, arguably, a better idea is to – set a class for the row and remove/change it using JS:您应该将显示值设置为
table-row
,而不是block
。Instead of
block
, you should set the display value totable-row
.将其设置为
table-row
或""
set it to
table-row
or to""
我通过添加/删除名为“隐藏”的类名来隐藏和显示表行。这种方法的优点是能够隐藏/显示任何元素,而无需知道或关心其正常显示样式是什么。对于表格行,这可能不是问题,但对于其他元素,您可能有一天希望从
block
更改为inline
。如果您使用类来隐藏元素,则无需更改 javascript。在你的CSS中:
在javascript中,
那个JS来自SO上的其他人,但不幸的是我忘记保存链接。
因此,要隐藏元素
addClass( document.getElementById('Asset'), 'hide' )
和removeClass
来显示它。编辑:我不知何故错过了安迪的回答。它本质上做同样的事情,但是,这些 addClass 和removeClass 方法比“手动”修改 element.className 更健壮一些。
I hide and show table rows by adding/removing a class name called 'hide'. This approach has the advantage of being able to hide/show any element without knowing or caring what its normal display style is. With table rows, that's probably a non-issue, but with other elements, you may one day wish to change from
block
toinline
for example. If you use a class to hide elements, you don't have to change your javascript.In your CSS:
And in javascript,
That JS came from someone else on SO, but unfortunately I forgot to save a link.
So to hide an element
addClass( document.getElementById('Asset'), 'hide' )
andremoveClass
to show it.EDIT: I missed Andy's answer somehow. It does essentially the same thing, however, these addClass & removeClass methods are a little more robust than modifying element.className "manually".