< script>何时应标签是可见的,为什么能显示? [英] When should <script> tags be visible and why can they?
问题描述
一个样式为display:block
的script
元素显示出来.为什么有可能呢?有没有实际需要的用例?
A script
element that got styled as display:block
appears visible. Why is it possible and is there any real use case where it is desired?
td > * {
display: block;
}
<table>
<tr>
<td>
<script type="text/javascript">
var test = 1;
</script>von 1
</td>
</tr>
</table>
推荐答案
HTML5规范第10.3.1节列出了隐藏元素"的样式:
The HTML5 specification defines a style sheet that user agents (like browsers) are expected to use. Section 10.3.1 lists the styles for "Hidden elements":
@namespace url(http://www.w3.org/1999/xhtml);
[hidden], area, base, basefont, datalist, head, link,
meta, noembed, noframes, param, rp, script, source, style, template, track, title {
display: none;
}
embed[hidden] { display: inline; height: 0; width: 0; }
如您所见,它将display: none;
应用于script
.
As you can see, it applies display: none;
to script
.
这是用户和隐藏的script
元素之间的唯一障碍".很好,可以覆盖作者样式表(和当然也包括在用户样式表中.
This is the only "barrier" between your users and hidden script
elements. It’s perfectly fine and intended to be able to overwrite styles from user-agent style sheets within author style sheets (and of course also within user style sheets).
为什么有人要使用它?一种用例是显示内容而不必转义<
/>
之类的字符,类似于旧的script
元素不仅可以用于脚本,而且可以也可以用于数据块(即,用于任何具有MIME的内容)类型).
Why someone might want to use it? One use case is displaying content without having to escape characters like <
/>
, similar to the old xmp
element. The script
element can be used not only for scripts, but also for data blocks (i.e., for anything with a MIME type).
这篇关于< script>何时应标签是可见的,为什么能显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!