使用 JavaScript 编写"Hello JavaScript!":
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
亲自试一试 »
这个<script>
标签用于嵌入客户端脚本(JavaScript)。
这个<script>
元素要么包含脚本语句,要么通过 src 属性指向外部脚本文件。
JavaScript 的常见用途是图片操作、表单验证和内容的动态更改。
提示:另请参阅<无脚本>元素适用于在浏览器中禁用了脚本或拥有不支持客户端脚本的浏览器的用户。
提示:如果您想了解有关 JavaScript 的更多信息,请访问我们的JavaScript 教程。
Element | |||||
---|---|---|---|---|---|
<script> | Yes | Yes | Yes | Yes | Yes |
Attribute | Value | Description |
---|---|---|
async | async | Specifies that the script is downloaded in parallel to parsing the page, and executed as soon as it is available (before parsing completes) (only for external scripts) |
crossorigin | anonymous use-credentials |
Sets the mode of the request to an HTTP CORS Request |
defer | defer | Specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing (only for external scripts) |
integrity | filehash | Allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated |
nomodule | True False |
Specifies that the script should not be executed in browsers supporting ES2015 modules |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin same-origin strict-origin strict-origin-when-cross-origin unsafe-url |
Specifies which referrer information to send when fetching a script |
src | URL | Specifies the URL of an external script file |
type | scripttype | Specifies the media type of the script |
在 XHTML 中,脚本内的内容被声明为 #PCDATA(而不是 CDATA),这意味着将解析实体。
这意味着在 XHTML 中,所有特殊字符都应该被编码,或者所有内容都应该包装在 CDATA 部分中:
<script type="text/javascript">
//<![CDATA[
let i = 10;
if (i < 5) {
// some code
}
//]]>
</script>
这个<script>
标签还支持HTML 中的全局属性。
HTML 教程:HTML 脚本
HTML DOM 参考:脚本对象
JavaScript 教程:学习 JavaScript
大多数浏览器都会显示<script>
具有以下默认值的元素:
script {
display: none;
}
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!