gpt4 book ai didi

jquery - 在 jQuery 中调用用户定义的函数

转载 作者:行者123 更新时间:2023-12-03 21:25:25 26 4
gpt4 key购买 nike

我试图在 jQuery 中调用用户定义的函数:

$(document).ready(function() {
$('#btnSun').click(function() {
myFunction();
});
$.fn.myFunction = function() {
alert('hi');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>

我也尝试了以下方法:

$(document).ready(function() {
$('#btnSun').click(function() {
myFunction();
});
});

function myFunction() {
alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>

好像不行啊!知道我哪里错了吗?

最佳答案

如果你想通过 jQuery 事件调用普通函数,你可以这样做:

$(document).ready(function() {
$('#btnSun').click(myFunction);
});

function myFunction() {
alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>

关于jquery - 在 jQuery 中调用用户定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520172/

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