天天看點

超簡單使用js的三種方式

菜鳥可以學會的使用js的三種方式 

超簡單使用js的三種方式

通過id使用js事件

<!DOCTYPE html>
<html>
<body>

<p>該執行個體聲明一個函數,函數調用時在 id="demo" 的元素上輸出 "Hello World" 。</p>

<p id="demo2"></p>

<script>
function myFunction() {
    document.getElementById("demo2").innerHTML = "Hello World!";
}

myFunction();
</script>

</body>
</html>      
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function myFunction(){
  alert("你好,我是一個警告框!");
}
</script>
</head>
<body>

<input type="button" onclick="myFunction()" value="顯示警告框" />

</body>
</html>      
<html>
<head>
<script type="text/javascript">
function getElements()
  {
  var x=document.getElementsByTagName("input");
  alert(x.length);
  }
</script>
</head>
<body>

<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<br />
<input type="button" onclick="getElements()"
value="How many input elements?" />

</body>
</html>