Jeo*_*Nam 1 jquery jquery-events
我试图在我的代码中使用 'on' 但失败了。我试过的代码是这样的:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
function clickHandler(e) {
alert('Click!');
}
$(document).on('ready', function() {
$('#click_me').on('click', clickHandler);
})
</script>
</head>
<body>
<input id="click_me" type="button" value="click me" />
</body>
</html>Run Code Online (Sandbox Code Playgroud)
虽然我可以用下面的代码替换这段代码。我仍然想知道为什么我的第一个事件代码on不起作用。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
function clickHandler(e) {
alert('Click!');
}
$(document).ready(function() {
$('#click_me').click(clickHandler);
})
</script>
</head>
<body>
<input id="click_me" type="button" value="click me" />
</body>
</html>Run Code Online (Sandbox Code Playgroud)