1

我有一个用 JavaScript 编写的方法,比如说“callme”,我在 $(document.ready) 块中编写了一些 jQuery 代码。

我的问题是如何从 jQuery 块中调用现有的 JavaScript 方法“callme”。

假设清单如下,

function callme(){
    // do some complex processing. I don't want to do this in jQuery
}

$(document).ready(function(){
    // I need to call callme function from here.
});

请帮帮我。

4

6 回答 6

9

$(document).ready(function(){ callme()});

于 2009-07-27T14:26:44.697 回答
4

It might be worth mentioning that there is also a shortcut available; simply $(callme);.

于 2009-07-27T14:30:41.463 回答
4
$(document).ready(callme);
于 2009-07-27T14:26:46.400 回答
3

Nothing special you need to do. The $(document).ready() call is just a function, so you can feel free to call your other functions in there.

Remember, jQuery is still javascript. Everything just runs through the jQuery function to handle all the custom methods and such. Anything you can do in javascript, you can do in jQuery.

于 2009-07-27T14:33:08.650 回答
2

Might be easier to read (it is for me, when things get more complicated), but exactly the same answer as Dave's:

$(document).ready( function()
{
     ...
     callme();
     ...
});
于 2009-07-27T14:30:42.180 回答
0

为什么不只做以下事情?

$(function(){
   // Do your processing here
});

您实际上并不需要创建特定的命名函数,除非您当然要在页面加载后多次执行它。

另外,“复杂处理”是什么意思?JavaScript 不是多线程的,因此一次只能执行一个函数。如果您的“复杂处理”需要很长时间,那么页面将在完成之前变得无响应。

于 2009-07-27T22:20:01.640 回答