<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- 点击按钮执行函数myFunction() -->
<button onclick="myFunction()">点击这里</button>
<!-- 鼠标点击执行 ID是but文本框的函数test; -->
<button id="but">点击这里</button>
<script>
function myFunction() {
// 在控制台输出“Hello World!”
alert("Hello World!");
}
but.onclick = function test() {
// 在控制台输出“Hello World2!”
alert("Hello World2!");
};
</script>
</body>
</html>
运行结果