天天看点

超简单使用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>