gpt4 book ai didi

javascript - 为什么我无法从 $(document).ready 调用外部函数

转载 作者:行者123 更新时间:2023-11-28 11:25:32 24 4
gpt4 key购买 nike

使用下面的代码,点击按钮不会调用handleData()函数,如果我使用匿名函数方式(在/* */内部),它就有效。

有人可以解释一下问题是什么吗?以及如何解决它?我想从 $(document).ready(...) 调用全局函数。

<html>
<head>
<script src="js/jquery/jquery-1.7.2.min.js"></script>

<script>
function handleData(data, status) {
$("#p1").text(data);
};

$(document).ready(function() {

$("button").click(function() {
$.get("testAsync?name=value", handleData(data, status));
});

/*
$("button").click(function() {
$.get("testAsync?name=value", function(data, status) {
$("#p1").text(data);
});
});
*/
});
</script>
</head>
<body>
<button>Send an HTTP GET</button>
<p id="p1"></p>
</body>
</html>

最佳答案

该函数会立即被调用,因为它后面有(arguments)

由于参数是按顺序排列的,因此只需单独传递 handleData 即可。

关于javascript - 为什么我无法从 $(document).ready 调用外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162153/

24 4 0
文章推荐: swift - 移除 NSOutlineView 周围的边框
文章推荐: jquery - 在 中将文本换行到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com