2013-03-03 73 views
2

我在想,如果我能避免我的HTML插入<script>代碼,並使用事件,而不是:的Javascript事件定義

<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*屬性我可以在#foo DIV用在哪裏可以插入我的JavaScript?

+3

你最終想達到什麼目的? – 2013-03-03 14:43:35

+0

初始加載時有一個事件(不在IE上,但jQuery可以模擬它)。 – 2013-03-03 14:43:43

+0

@lee,儘快調整'#foo' div的類 – Alex 2013-03-03 14:44:10

回答

1

不幸的是,沒有。它可以是最緊湊的是<script>initSomething();</script>你想要的事件。

如果這不是某種插件,請嘗試將其添加到<body>。這是「標準」。

+0

突變觀察員不能使用? – 2013-03-03 14:44:54

+0

@JanDvorak實際上沒有,沒有。 https://developer.mozilla.org/zh-CN/docs/DOM/Mutation_events#Cross-browser_support – 2013-03-03 14:45:48

+0

@JanDvorak:那存在? – Ryan 2013-03-03 14:45:56