简体   繁体   English

如何从 HREF 调用 $(window).load 中定义的 JQuery function

[英]How to call a JQuery function defined in $(window).load from an HREF

I like to scope all of my JQuery functions and event sinks to $(window).load, like this:我喜欢 scope 我所有的 JQuery 函数和事件汇到 $(window).load,像这样:

$(window).load(function ()
{
   function Foo(id)
   {
      alert(String.format("Do Foo for: {0}", id));
   }
});

Normally, I do all my work at this scope, but I have a case where I'd like to call Foo(27) from an HREF built by a standalone JQuery widget.通常,我在此 scope 上完成所有工作,但我有一个案例,我想从由独立 JQuery 小部件构建的 HREF 调用 Foo(27)。 The HREF looks like this: HREF 如下所示:

<a href="javascript:Foo(27)">Click me!</a>

However, the Foo() function isn't found when I click on the link.但是,当我单击链接时,找不到 Foo() function。 How can I make it so?我怎样才能做到这一点?

[EDIT] [编辑]

So, I just accepted an answer below and wanted to share my final implementation.所以,我刚刚接受了下面的答案,并想分享我的最终实现。 Foo() has been replaced with the name of actual method I'm using. Foo() 已替换为我正在使用的实际方法的名称。 And yes, I know that String.format() doesn't exist natively;是的,我知道 String.format() 本身并不存在; it's part of my own base library.它是我自己的基础库的一部分。 With that in mind, here's what I've got.考虑到这一点,这就是我所拥有的。 This is from the file where I define all of my global namespaces.这是来自我定义所有全局名称空间的文件。 This definition exists outside the jQuery scope.此定义存在于 jQuery scope 之外。

// This is defined in the global namespace
if (typeof (window.App) === "undefined")
{
   window.App = {};
}

Here's the line from the jQuery widget that builds the HREF.这是构建 HREF 的 jQuery 小部件中的行。 The title is for a prediction, and the HREF navigates to the details page for that prediction:标题用于预测,HREF 导航到该预测的详细信息页面:

r = String.format('<a href="javascript:App.NavDetails({1});" class="link3">{0}</a>',
   Predikt.General.EncodeHtml(options.rowData.Title), 
   options.rowData.PredictionId);

And here's the actual implementation of the NavDetails function in my $(window).load()-scoped jQuery file:这是 NavDetails function 在我的 $(window).load() 范围的 jQuery 文件中的实际实现:

$(window).load(function ()
{
   App.NavDetails = function (id)
   {
      // Do something interesting with the ID here...
      alert(String.format("The ID is: {0}", id));
   };
});

You can do this你可以这样做

$(window).load(function (){
    window.Foo =  function(n){
      alert(n);
   }
});

http://jsfiddle.net/JQn8H/ http://jsfiddle.net/JQn8H/

Or this或这个

var Foo;

$(window).load(function (){
    Foo =  function (n){
      alert(n);
   }
});

http://jsfiddle.net/JQn8H/2/ http://jsfiddle.net/JQn8H/2/

IMO, a better approach would be to set a namespace for your app, so you don't pollute the gobal namespace IMO,更好的方法是为您的应用程序设置一个命名空间,这样您就不会污染全局命名空间

window.App = {};

$(window).load(function (){
   App.Foo =  function (n){
      alert(n);
   }
});

Then, in your html you would call it:然后,在您的 html 中,您可以调用它:

<a href="javascript:App.Foo(27)">Click me!</a>

http://jsfiddle.net/JQn8H/3/ http://jsfiddle.net/JQn8H/3/

But, you might want to consider calling it from your script and not from the markup.但是,您可能需要考虑从脚本而不是标记中调用它。

Either move the Foo function outside your $(window).load() function or bind Foo to your <a> element inside the loading function.Foo function 移到$(window).load() function 之外,或者将Foo绑定到加载 ZC1C425268E68385D1AB5074C17A94F14 内的<a>元素。 You can't get inside your $(window).load() callback from the outside.您无法从外部进入您的$(window).load()回调。


From your comments it looks like you need Foo to stay inside your $(window).load() anonymous function as it needs to access other things in that scope.从您的评论看来,您需要Foo留在您的$(window).load()匿名 function 中,因为它需要访问 scope 中的其他内容。 You can work around this by moving a reference to Foo outside the anonymous function.您可以通过在匿名 function 之外移动对Foo的引用来解决此问题。 This variant of jm-'s approach:这种jm- 方法的变体:

$(window).load(function() {
    //...
    function Foo() {
        //...
    }
    //...
    window.Foo = Foo;
});

This leaves a reference to Foo in the window object and since window is the default scope, you should be able to access it in your <a> element.这会在window object 中留下对Foo的引用,并且由于window是默认的<a> > 元素,您应该能够访问它。

You have to choose between doing work at that scope and having a function accessible like that.您必须在 scope 和 function 之间进行选择。 The only other alternative is to force a reload or to bind it to the anchor's click handler.唯一的其他选择是强制重新加载或将其绑定到锚点的单击处理程序。

Also, you might have changed prototype, but unless you have, String.format doesn't work.此外,您可能已经更改了原型,但除非您更改了,否则String.format不起作用。 You probably just want to do "Do Foo for: " + id你可能只想做"Do Foo for: " + id

function Foo(id) {
  alert("Do Foo for: " + id);
}
$(window).load( ... );

or或者

<a class="someClass" data-id="23">Click me!</a>

...

$(window).load(function() {
  $("a.someClass").click(function() {
    alert("Do Foo for: " + $(this).data("id"));
  });
});

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

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