javascript - 如何在javascript中优先执行dom

标签 javascript html dom

当我想在 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/

相关文章:

javascript - 使用 DOM 将 SVG 元素添加到现有 SVG

javascript - 将元素从数组插入到现有 Html 元素的有效方法是什么?

javascript - 错误 :InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number

javascript - Bxslider字幕动画

javascript - ReasonML 是否支持异步/等待?

php - HTML 表单发送到 MYSQL DB 时出现 PHP POST 错误

html - Wordpress 中的响应式元素

javascript - 使用 jQuery .load() 仅接收图像的字节码

javascript - childNode 意外行为

javascript - 使用 AJAX 查询进行按键按下与更改时比较 : which is better?