编辑:解释后添加了 HTML5 注释。
以下标签具有与之关联的任何 on* 事件(即 onkeypress)(按字母顺序列出):
a, abbr, acronym, address, area, b, bdo, big, blockquote, body, button, caption, cite, code, col, colgroup, dd, del, dfn, div, dl, dt, em, fieldset , form, h1, h2, h3, h4, h5, h6, hr, i, img, input, ins, kbd, label, legend, li, link, map, noscript, object, ol, optgroup, option, p, pre , q, samp, select, small, span, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, ul, var
以下标签没有有任何on*事件:
base、br、head、html、meta、param、脚本、样式、标题
根据XHTML 1.0 Strict DTD。元素的属性列表包含attrs 或events 实体,例如(第二行):
<!ATTLIST img
%attrs;
src %URI; #REQUIRED
alt %Text; #REQUIRED
longdesc %URI; #IMPLIED
height %Length; #IMPLIED
width %Length; #IMPLIED
usemap %URI; #IMPLIED
ismap (ismap) #IMPLIED
>
分解如下...首先你的 onkeypress 事件在实体%events中定义:
<!ENTITY % events
"onclick %Script; #IMPLIED
ondblclick %Script; #IMPLIED
onmousedown %Script; #IMPLIED
onmouseup %Script; #IMPLIED
onmouseover %Script; #IMPLIED
onmousemove %Script; #IMPLIED
onmouseout %Script; #IMPLIED
onkeypress %Script; #IMPLIED
onkeydown %Script; #IMPLIED
onkeyup %Script; #IMPLIED"
>
然后在实体%attrs中被别名:
<!ENTITY % attrs "%coreattrs; %i18n; %events;">
这两个实体在 DTD 的其他任何地方都没有引用。然后搜索包含至少一个这些实体的任何 ATTLIST。
另一方面,HTML5 不是基于 SGML,它不能有 DTD;见Where is the HTML5 Document Type Definition?。因此,我对 XHTML 1.0 所做的分析不适用于 HTML5。
According to the HTML5 spec,任何和所有元素都可以应用事件:
[E]event 处理程序(及其相应的事件处理程序事件类型)...必须被所有 HTML 元素支持,作为事件处理程序内容属性和事件处理程序 IDL 属性;和 ... 必须被所有 Document 和 Window 对象支持,作为事件处理程序 IDL 属性。
在事件表中包含您的onkeypress 事件处理程序,事件类型为keypress。
理论上,任何事件都可以在任何element node (node type ELEMENT_NODE 1) 上运行,但我不会指望它在所有浏览器中使用。我会说坚持使用 XHTML 实现会更安全。
我使用以下 PHP 脚本来解析和编译列表:
<?php
$dtd = '/path/to/local/copy/of/xhtml1-strict.dtd';
$file = file_get_contents($dtd);
$match = '/(<!ATTLIST[^>]+%(attrs|events);[^>]*>)/i';
preg_match_all($match,$file,$matches);
$tags = array();
if ( isset($matches[0]) ) {
foreach ( $matches[0] as $attlist ) {
preg_match('/ATTLIST\s([^\s]+)\s/',$attlist,$tag);
if ( isset($tag[1]) && $tag[1] ) {
$tags[] = $tag[1];
}
}
}
sort($tags);
$element = '/<!ELEMENT\s([^\s]+)/i';
preg_match_all($element,$file,$elements);
$elements = $elements[1];
sort($elements);
echo 'Has Events: '.implode(', ',$tags);
echo "\n\n";
echo 'Missing Events: '.implode(', ',array_diff($elements,$tags));
?>