这会触发 onload 事件:
<!DOCTYPE html>
<html>
<body onload="alert('Hello')">
<p> Demo </p>
</body>
</html>
这不会触发 onload 事件:
<!DOCTYPE html>
<html>
<body>
<p id="demo" onload="alert('Hello')"> Demo </p>
</body>
</html>
在第二个示例中,为什么事件没有触发?
支持的元素onload
are
<body>, <frame>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>
加载时访问任何其他元素的方法可以是添加脚本标记,如下所示,尽管这在很大程度上取决于您想要实现的目标
<!DOCTYPE html>
<html>
<body>
<p id="demo"> Demo </p>
<script>console.log( document.getElementById('demo').textContent );</script>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)