定义元素后的Javascript事件

时间:2013-03-03 14:42:08

标签: javascript html

我想知道是否可以避免在HTML中插入<script>标记,而是使用事件:

<div id="foo">
    <script>
       document.getElementById('foo').className = 'has-js';
       // ^^ can I put this in some on* attribute of #foo,
       // and make it run at this point ?
    </script>

    ...   
    <div ...

我可以在on* div上使用一些#foo属性,我可以插入我的javascript吗?

1 个答案:

答案 0 :(得分:1)

不幸的是,没有。最紧凑的可能是<script>initSomething();</script>你想要的事件。

如果这不是某种插件,请尝试将其添加到<body>。这是“标准”。