当我想在 html 标记中 document.getElementById();
时遇到问题。
示例 =
<html>
<head>
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
</head>
<body>
<div> ...
<div id="test">
</div>
</body>
</html>
我的问题是如何在不等待加载脚本的情况下执行 document.getElementById("test");
?这可以节省更多时间。我该如何解决这个问题。谢谢您的回答。
注意:解决不删除或修改脚本到找到的目录。 以防万一,如果找不到脚本。
最佳答案
添加defer="defer"
给那些<script />
您要延迟执行的标签。
注意:defer 适用于 IE 以及更新版本的 Firefox 和 Chrome
关于javascript - 如何在javascript中优先执行dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8267503/