问题
When an event, such as onclick, is declared in a col element for an HTML table does that event impact the cells referenced by the col element? Is the event ignored? Does something else happen?
回答1:
Great question.
The spec says:
<!ATTLIST COL -- column groups and properties --
%attrs; -- %coreattrs, %i18n, %events ---
where %events says:
<!ENTITY % events
"onclick %Script; #IMPLIED -- a pointer button was clicked --
ondblclick %Script; #IMPLIED -- a pointer button was double clicked--
onmousedown %Script; #IMPLIED -- a pointer button was pressed down --
onmouseup %Script; #IMPLIED -- a pointer button was released --
onmouseover %Script; #IMPLIED -- a pointer was moved onto --
onmousemove %Script; #IMPLIED -- a pointer was moved within --
onmouseout %Script; #IMPLIED -- a pointer was moved away --
onkeypress %Script; #IMPLIED -- a key was pressed and released --
onkeydown %Script; #IMPLIED -- a key was pressed down --
onkeyup %Script; #IMPLIED -- a key was released --"
>
So yes, it appears events are supposed to be supported for the td
s.
However "supposed to" is a far cry from "does". The only way to know is to test in all browsers and hope it works in future browsers too.
On second thought, just use event delegation and bind once to the parent table
instead. That will work now and forever more.
来源:https://stackoverflow.com/questions/1406399/how-are-cells-of-a-table-impacted-by-declaring-an-event-in-a-col-element