Javascript 我可以在 <TABLE> 中有 <SCRIPT> 标签吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3658279/
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
Can I have <SCRIPT> tag inside <TABLE>?
提问by fast-reflexes
I got to have a tag inside a table because PHP writes a code there that adds stuff to an earlier created Javascript array. However, I get a validation error (4.01 strict).
我必须在表中有一个标签,因为 PHP 在那里编写了一个代码,将内容添加到之前创建的 Javascript 数组中。但是,我收到验证错误(严格为 4.01)。
Is there any way I can do this or is it simply forbidden to keep a script like this:
有什么办法可以做到这一点,或者只是禁止保留这样的脚本:
<TABLE>
<TR>
<SCRIPT></SCRIPT>
<TD>
</TD>
</TR>
</TABLE>
(is this better maybe?):
(这可能更好吗?):
<TABLE>
<TR>
<TD>
<SCRIPT></SCRIPT>
</TD>
</TR>
</TABLE>
Change doctype? What do you think?
更改文档类型?你怎么认为?
采纳答案by Mark Amery
Ever since the release of HTML 5, it islegal to have a <script>inside most kinds of table element, including trs.
自从 HTML 5 发布以来,在大多数类型的表格元素中都有一个内部元素是合法的<script>,包括trs。
At https://html.spec.whatwg.org/multipage/tables.html, you can see that the content models for table, caption, tbody, thead, tfoot, tr, tdand thall include either "Flow content"or "one or more script-supporting elements". At https://html.spec.whatwg.org/multipage/dom.html#flow-content-2and https://html.spec.whatwg.org/multipage/dom.html#script-supporting-elements-2respectively, we see that scripts are defined to be both flow content and script-supporting elements, and are therefore allowed within any of those elements.
在https://html.spec.whatwg.org/multipage/tables.html,你可以看到内容模型table,caption,tbody,thead,tfoot,tr,td和th所有的包括两种“流内容”或“一个或多个脚本的支持元素”。在https://html.spec.whatwg.org/multipage/dom.html#flow-content-2和https://html.spec.whatwg.org/multipage/dom.html#script-supporting-elements-2分别,我们看到scripts 被定义为流内容和脚本支持元素,因此允许在任何这些元素中。
The notable exceptions are colgroups (which can only contain cols and templates) and cols (which can't have content). You can't put a scriptinside either of those.
值得注意的例外是colgroups(只能包含cols 和templates)和cols(不能有内容)。你不能script在其中任何一个里面放一个。
This means that the asker's example HTML with a script inside a tr...
这意味着提问者的示例 HTML 中包含一个脚本tr...
<TABLE>
<TR>
<SCRIPT></SCRIPT>
<TD>
</TD>
</TR>
</TABLE>
... is now valid, which you can confirm at https://html5.validator.nu.
... 现在有效,您可以在https://html5.validator.nu确认。
Gumbo's answer, stating that scripts are allowed inside tds but not trs, was correct for HTML 4, but is now outdated.
Gumbo 的回答指出scripts 允许在tds 内但不允许在s内tr,对于 HTML 4 是正确的,但现在已经过时了。
回答by Gumbo
SCRIPTis not allowed in TRas the content model of TRis defined as (TH|TD)+:
SCRIPT不允许进入,TR因为 的内容模型TR定义为(TH|TD)+:
<!ELEMENT TR - O (TH|TD)+ -- table row -->
That means one or more elements of THor TD.
这意味着一个或多个元件TH或TD。
But SCRIPTis allowed in TD. See the definition of TD:
但SCRIPT允许进入TD。见定义TD:
<!ELEMENT (TH|TD) - O (%flow;)* -- table header cell, table data cell-->
Where the parameter entity flowis defined as:
其中参数实体流定义为:
<!ENTITY % flow "%block; | %inline;">
And inlineis defined as:
而内嵌的定义为:
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
And specialis define as:
而特别的是定义为:
<!ENTITY % special
"A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">

