HTML script
JavaScript makes HTML pages more dynamic and interactive.
HTML <script> tag
##<script> tag is used to define client-side scripts, such as JavaScript. The <script> element can contain script statements or point to an external script file through the src attribute. JavaScript is most commonly used for image manipulation, form validation, and dynamic content updating.Example
The following script will output "How are you?" to the browser:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <script> document.write("你好嗎?") </script> </body> </html>Program running result:
How are you?
To learn more about Javascript tutorials, check out JavaScript Tutorials!
##HTML<noscript> tag The <noscript> tag provides alternative content when scripting is not available, such as when scripting is disabled in the browser, or when the browser does not support client-side scripting. The
<noscript> element can contain all elements found within the body element of a normal HTML page.
Only when the browser does not support scripts or scripts are disabled, the content in the <noscript> element will be displayed:
Example<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<script>
document.write("Hello World!")
</script>
<noscript>抱歉,你的瀏覽器不支持 JavaScript!</noscript>
<p>不支持 JavaScript 的瀏覽器會使用 <noscript> 元素中定義的內(nèi)容(文本)來替代。</p>
</body>
</html>
Program running result:
JavaScript experience (from JavaScript tutorial on this site)JavaScript example code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p> JavaScript 能夠直接寫入 HTML 輸出流中: </p> <script> document.write("<h1>這是一個標(biāo)題</h1>"); document.write("<p>這是一個段落。</p>"); </script> <p> 您只能在 HTML 輸出流中使用 <strong>document.write</strong>。 如果您在文檔已加載后使用它(比如在函數(shù)中),會覆蓋整個文檔。 </p> </body> </html>
Program execution result:
JavaScript event response:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <h1>我的第一個 JavaScript </h1> <p id="demo"> JavaScript 可以觸發(fā)事件,就像按鈕點擊。</p> <script> function myFunction() { document.getElementById("demo").innerHTML="Hello JavaScript!"; } </script> <button type="button" onclick="myFunction()">點我</button> </body> </html>
Run the program and see
JavaScript processing HTML style:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <h1>我的第一段 JavaScript</h1> <p id="demo"> JavaScript 能改變 HTML 元素的樣式。 </p> <script> function myFunction() { x=document.getElementById("demo") // 找到元素 x.style.color="#ff0000"; // 改變樣式 } </script> <button type="button" onclick="myFunction()">點擊這里</button> </body> </html>