gpt4 book ai didi

jquery - read() 工作,在 ('ready' 上)不工作,为什么?

转载 作者:行者123 更新时间:2023-12-01 06:43:26 24 4
gpt4 key购买 nike

我尝试在代码中使用“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>

虽然我可以用下面的代码替换这个代码。我仍然想知道为什么我的第一个带有事件 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>

最佳答案

来自 on() 的文档,强调我的:

There is also $(document).on("ready", handler), deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.

关于jquery - read() 工作,在 ('ready' 上)不工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53651676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com