我想知道是否可以避免在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吗?
答案 0 :(得分:1)
不幸的是,没有。最紧凑的可能是<script>initSomething();</script>
你想要的事件。
如果这不是某种插件,请尝试将其添加到<body>
。这是“标准”。