简体   繁体   English

jQuery和onclick html函数回调

[英]jQuery and onclick html function callback

Let's say we have like 3 functions: 假设我们有3个函数:

// Notice there are no $(document).ready function
function third(par)
{
    // uses jQuery.fadeIn() and jQuery.fadeOut()
}

function second()
{
    // do normal javascript
    third('Something')
}

function main()
{
   var response = $.ajax() // an ajax call with jQuery
}

Now let's take a look at an HTML page 现在让我们看一下HTML页面

<button onclick="main()">Click me, please</button>

It seems that functions define inside $(document).ready are unavailable out side, so main() will be undefined if putted inside $(document).ready I've checked the actions of each function with FireBug and i can tell you this: 似乎函数在$(document).ready内部定义是不可用的,所以如果放在$(document).readymain()将是未定义的$(document).ready我已经用FireBug检查了每个函数的动作,我可以告诉你这个:

  • The main() function is called and the ajax call called returning the right data. 调用main()函数,调用ajax调用返回正确的数据。
  • The second() function is called successfully second()函数被成功调用
  • The third() function is called but an error occur: fadeIn() is not defined as any other jQuery functions. 调用third()函数但发生错误:fadeIn()未定义为任何其他jQuery函数。

My question is: what should i do to attach a jQuery function to an HTML onclick event caller? 我的问题是:如何将jQuery函数附加到HTML onclick事件调用者?

$ ('button').click(main); $ ('button').click(main); inside document ready 内部文件准备好了

or 要么

var main;

$(document).ready(function(){
main = function(){...}
...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM