簡介
javascript :是一個跨平臺的腳本語言;是一種輕量級的編程語言。
JavaScript 是 Web 的編程語言。所有現代的 HTML 頁面都使用 JavaScript。
HTML: 結構
css: 表現
JS: 行為
HTML+CSS 只能稱之為靜態網頁,加入js網頁則有了靈魂稱之為動態網頁
腳本語言的特點:
不能獨立運行,要依賴網頁;
可插入 HTML 頁面的編程代碼。
插入 HTML 頁面后,可由所有的現代瀏覽器執行。
javascript 在網頁中使用的兩種方式:
方式1:直接在script標簽中編寫;
方式2:引用外部js代碼
注意:
1.可以有多個script標簽,多個script標簽都是自上而下順序執行
2.alert()的作用:控制瀏覽器彈出一個警告對話框
方式1實例:直接在script標簽中編寫
1.新建一個html文件(如:demo.html),并將下述內容寫入文件中
<!DOCTYPE HTML><html><title>hello world</title><body></body>
</html>
2.在html文件中插入一個script標簽
<script>alert("hello world"); </script>
<script>alert("hello world-2"); </script>
?
3.將html文件用瀏覽器打開,即可看見hello world
點擊確定后,可以看見hello world-2
demo.html完整文件內容如下:
<!DOCTYPE HTML>
<html><title>hello world</title><body><script>alert("hello world"); </script><script>alert("hello world-2"); </script></body>
</html>
方式2實例:引用外部js代碼
1.新建一個html文件(如:demo.html),并將下述內容寫入文件中
<!DOCTYPE HTML><html><title>hello world</title><body></body>
</html>
2.在html文件(如:demo.html)同目錄下新建一個js文件(如:demojs.js),并將下述內容寫入文件中
alert("hello world");
3.在html文件中引用外部js代碼(如:demojs.js)
<script src="./demojs.js">alert("hello world-2");</script>
<script>alert("hello world-3");</script>
?
4.將html文件用瀏覽器打開,即可看見hello world
點擊確定后,可以看見hello world-3
demo.html完整文件內容如下:
<!DOCTYPE HTML>
<html><title>hello world</title><body><script src="./demojs.js">alert("hello world-2");</script><script>alert("hello world-3");</script></body>
</html>
注意:script標簽一旦引入外部文件,就不能編寫代碼了,即使寫了瀏覽器也會自動忽略。
如果需要再創建一個script代碼即可。