簡介
javascript :是一個跨平臺的腳本語言;是一種輕量級的編程語言。
JavaScript 是 Web 的編程語言。所有現(xiàn)代的 HTML 頁面都使用 JavaScript。
HTML: 結(jié)構(gòu)
css: 表現(xiàn)
JS: 行為
HTML+CSS 只能稱之為靜態(tài)網(wǎng)頁,加入js網(wǎng)頁則有了靈魂稱之為動態(tài)網(wǎng)頁
腳本語言的特點(diǎn):
不能獨(dú)立運(yùn)行,要依賴網(wǎng)頁;
可插入 HTML 頁面的編程代碼。
插入 HTML 頁面后,可由所有的現(xiàn)代瀏覽器執(zhí)行。
javascript 在網(wǎng)頁中使用的兩種方式:
方式1:直接在script標(biāo)簽中編寫;
方式2:引用外部js代碼
注意:
1.可以有多個script標(biāo)簽,多個script標(biāo)簽都是自上而下順序執(zhí)行
2.alert()的作用:控制瀏覽器彈出一個警告對話框
方式1實(shí)例:直接在script標(biāo)簽中編寫
1.新建一個html文件(如:demo.html),并將下述內(nèi)容寫入文件中
<!DOCTYPE HTML>
<html>
<title>hello world</title>
<body>
</body>
</html>
2.在html文件中插入一個script標(biāo)簽
<script>alert("hello world"); </script>
<script>alert("hello world-2"); </script>
?
3.將html文件用瀏覽器打開,即可看見hello world
點(diǎn)擊確定后,可以看見hello world-2
demo.html完整文件內(nèi)容如下:
<!DOCTYPE HTML>
<html>
<title>hello world</title>
<body>
<script>alert("hello world"); </script>
<script>alert("hello world-2"); </script>
</body>
</html>
方式2實(shí)例:引用外部js代碼
1.新建一個html文件(如:demo.html),并將下述內(nèi)容寫入文件中
<!DOCTYPE HTML>
<html>
<title>hello world</title>
<body>
</body>
</html>
2.在html文件(如:demo.html)同目錄下新建一個js文件(如:demojs.js),并將下述內(nèi)容寫入文件中
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
點(diǎn)擊確定后,可以看見hello world-3
demo.html完整文件內(nèi)容如下:
<!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標(biāo)簽一旦引入外部文件,就不能編寫代碼了,即使寫了瀏覽器也會自動忽略。文章來源:http://www.zghlxwxcb.cn/news/detail-660419.html
如果需要再創(chuàng)建一個script代碼即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-660419.html
到了這里,關(guān)于js簡介以及在html中的2種使用方式(hello world)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!