​菜鸟可以学会的使用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>

  

 通过onclick使用js事件

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

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

</body>
</html>

  

  直接给html标签设定js事件

<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>

  

posted @ 2022-04-23 21:22  查科技在线  阅读(31)  评论(0编辑  收藏  举报