gpt4 book ai didi

javascript - 从 javascript 调用 Jquery 函数

转载 作者:可可西里 更新时间:2023-11-01 01:55:17 26 4
gpt4 key购买 nike

如何从 javascript 调用 jQuery 函数?

 //jquery
$(function() {

function my_fun(){
/.. some operations ../
}
});

//just js
function js_fun () {

my_fun(); //== call jquery function
}

最佳答案

是的,你可以(这就是我对原始问题的理解)。我是这样做的。只需将其与外部环境联系起来即可。例如:

//javascript

my_function = null;

//jquery
$(function() {

function my_fun(){
/.. some operations ../
}
my_function = my_fun;
})

//just js
function js_fun () {
my_function(); //== call jquery function - just Reference is globally defined not function itself
}

我在尝试访问实例化对象的方法时遇到了同样的问题仅在 DOM 对象就绪时。作品。我的例子:

MyControl.prototype = {
init: function {
// init something
}
update: function () {
// something useful, like updating the list items of control or etc.
}
}

MyCtrl = null;

// create jquery plug-in
$.fn.aControl = function () {
var control = new MyControl(this);
control.init();
MyCtrl = control; // here is the trick
return control;
}

现在你可以使用一些简单的东西,比如:

function() = {
MyCtrl.update(); // yes!
}

关于javascript - 从 javascript 调用 Jquery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1345722/

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