HTML按钮的定义方法如下:
<button>Your Text</button>
想要让按钮点击后做出某些事件,用到onclick函数就行,里面写一个JavaScript函数。
<button onclick="function()">Text</button>
提示:若函数只写一个代码,如alert("Hello World!");,只用写出代码,不用另起函数。
比如,我们来写一个小例子:
<html>
<head>
<title>检测能不能上清华</title>
</head>
<style>
body {
background-color:skyblue;
}
</style>
<body>
<center>
<h1 style="font-family:宋体;">检测能不能上清华</h1>
<button onclick="judge()">检测</button>
<h2 id="demo" style="color:red;"></h2>
</center>
</body>
<script>
//新函数
function judge() {
var b = confirm("你爱玩Mc吗?");
if (b==true){
document.getElementById.innerHTML="可以!";
} else {
document.getElementById.innerHTML="不可以!";
}
}
</script>
</html>