js单击事件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>helloworld</title>
<script type="text/javascript">
window.onload = function ()
{
var butObj = document.getElementById("but01");
butObj.onclick = function ()
{
alert("js单击事件");
}
};
</script>
</head>
<body>
<button id="but01">SayHello</button>
</body>
</html>
用jQuery实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function (){ //相当于window.onload = function(){};表示页面加载完成
var $butObj = $("#but01");//表示按id获得了该标签的DOM对象
$butObj.click(function (){ //表示绑定单击事件
alert("jQuery单击事件");
});
});
</script>
</head>
<body>
<button id="but01">SayHello</button>
</body>
</html>
- 要使用jQuery一定要导入jQuery库
- $是一个函数