javaScript示例

 1 <p>
 2 JavaScript 能够直接写入 HTML 输出流中:
 3 </p>
 4 
 5 <script>
 6 document.write("<h1>This is a heading</h1>");
 7 document.write("<p>This is a paragraph.</p>");
 8 </script>
 9 
10 <p>
11 您只能在 HTML 输出流中使用 <strong>document.write</strong>12 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
13 </p>
1 <h1>我的第一段 JavaScript</h1>
2 
3 <p>
4 JavaScript 能够对事件作出反应。比如对按钮的点击:
5 </p>
6 
7 <button type="button" onclick="alert('Welcome!')">点击这里</button>
 1 <h1>我的第一段 JavaScript</h1>
 2 
 3 <p id="demo">
 4 JavaScript 能改变 HTML 元素的内容。
 5 </p>
 6 
 7 <script>
 8 function myFunction()
 9 {
10 x=document.getElementById("demo");  // 找到元素
11 x.innerHTML="Hello JavaScript!";    // 改变内容
12 }
13 </script>
14 
15 <button type="button" onclick="myFunction()">点击这里</button
 1 <h1>我的第一段 JavaScript</h1>
 2 
 3 <p>请输入数字。如果输入值不是数字,浏览器会弹出提示框。</p>
 4 
 5 <input id="demo" type="text">
 6 
 7 <script>
 8 function myFunction()
 9 {
10 var x=document.getElementById("demo").value;
11 if(x==""||isNaN(x))
12     {
13     alert("Not Numeric");
14     }
15 }
16 </script>
17 
18 <button type="button" onclick="myFunction()">点击这里</button>
<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script>
<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
原文地址:https://www.cnblogs.com/the-wang/p/7507920.html