การเขียน JavaScript ในเว็บ
การเขียน JavaScript ในเว็บ เขียนโดยการใช้แท็ก <script> ดังนี้
<script>
//คำสั่งต่างๆ
</script>
ซึ่งสามารถแทรกตามส่วนต่างๆภายในเพจดังนี้
การแทรกใน <header>
<!DOCTYPE html><br />
<html><br />
<head><br />
<script><br />
function welcome() {<br />
document.getElementById(" innerhtml="ยินดีต้อนรับ" test="">
}
</script>
</head>
<body>
<h1>ทดสอบ</h1>
<p id="test">Hello สวัสดี</p>
<button type="button" onclick="welcome()">ทักทาย</button>
</body>
</html>
ผลลัพท์:Hello สวัสดี
ยินดีต้อนรับ
การแทรกในแท็ก <body>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>ทดสอบ</h1>
<p id="test">Hello สวัสดี</p>
<button type="button" onclick="welcome()">ทักทาย</button>
<script>
function welcome() {
document.getElementById("test").innerHTML = "ยินดีต้อนรับ";
}
</script>
</body>
</html>
การใช้สคริปต์จากภายนอก External JavaScript
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>ทดสอบ</h1>
<p id="test">Hello สวัสดี</p>
<button type="button" onclick="welcome()">ทักทาย</button>
<script src="myScript.js"></script>
</body>
</html>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น