<!doctype html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" defer></script>
<script defer> // Can I use defer atttribute here?
//Inline js code
</script>
</head>
<body>
</body>
</html>
这里可以使用defer属性来内联JS吗?
最佳答案
这是不可能的。每MDN :
defer: This attribute must not be used if the src attribute is absent (i.e. for inline scripts), in this case it would have no effect.
为了获得您想要的功能,您必须执行以下操作之一:
- 将脚本放入单独的文件中并设置
src
- 将内联脚本放在
<body>
的末尾 - 将脚本的整个代码封装在 DOMContentLoaded 监听器中
关于javascript - 我可以在内联js中使用defer属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60070377/