Javascript書寫位置

1.行內式js(不多使用)html

以on開頭,如onclickspa

HTML中推薦雙引號,JS推薦單引號code

2.內嵌式js(經常使用)htm

<script>blog

     alert('hello world');ip

<script>input

3.外部js文件it

<script src="my.js"></script>class

利於代碼結構化 便於文件複用cli

引用外部JS文件的script標籤中間不能夠寫代碼

適合js代碼量比較大的狀況下

 

代碼舉例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 2.內嵌式的JS -->
    <script>
        // alert('沙漠駱駝S')
    </script>
    <!-- 3.外部js寫法  script 雙標籤 -->
    <script src="my.js">
    </script>
</head>
<body>
    <!-- 1.行內式的JS 直接寫到元素內部 -->
    <!-- <input type="text" value="唐伯虎" onclick="alert('秋香姐')"> -->
</body>
</html>
相關文章
相關標籤/搜索