jQuery - $(document).ready 函数中的函数

发布于 2024-11-25 09:31:39 字数 773 浏览 2 评论 0原文

像这样在 .ready() 内部创建函数是否正确

$(document).ready(function() {

$(document).ready(function() {
     function callMe() {

     }
 });

在 DOM 准备好并且在 ready() 内部发生事件之前,.ready() 内部的函数不必调用> 被触发。

只是为了澄清一点 - 这是说明问题的代码:

$(function() {
    var ind = 0;

    // some event is executed and changes the value of the ind

    // another event which affects the ind variable

    // and another one - after this event we call our function


    // there's another event - and we call our function again

我需要调用的函数需要 ind 变量的更新值 - 我想我可以将其作为参数传递,但是有更好的方法吗?

另外 - 另一件重要的事情是,所讨论的 function() 还可以更改 ind 变量的值 - 例如递增它 (ind++ )。

Is it correct to create functions inside of

$(document).ready(function() {

like so:

$(document).ready(function() {
     function callMe() {

     }
 });

The function inside of the .ready() does not have to call before DOM is ready and event inside of the ready() is triggered.

Just to clarify a little bit - here's the code which would illustrate the problem:

$(function() {
    var ind = 0;

    // some event is executed and changes the value of the ind

    // another event which affects the ind variable

    // and another one - after this event we call our function


    // there's another event - and we call our function again

The function which I need to call needs the updated value of the ind variable - which I guess I could pass as a parameter, but is there a better way of doing it?

Also - another important thing is that the function() in question can also change the value of the ind variable - for instance incrementing it (ind++).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小猫一只 2024-12-02 09:31:40
<input type="button" value="Click Me!" onclick="Demo();" />


<script>

    $(document).ready(function () {

        Demo = function () {
            alert("Hello World");
        };
    });

</script>
<input type="button" value="Click Me!" onclick="Demo();" />


<script>

    $(document).ready(function () {

        Demo = function () {
            alert("Hello World");
        };
    });

</script>
迷雾森÷林ヴ 2024-12-02 09:31:39

是的,您可以这样做,这只是范围

如果您只需要从 $(document).ready(function() { }) 中访问 callMe(),那么可以将函数放在那里,并提供某些架构的好处是因为您无法访问该上下文之外的函数。

如果您需要在文档就绪之外使用 callMe() 函数,则需要在该上下文之外定义 callMe() 函数。

function callMe() {
  // Do Something
}

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

更新

根据您的澄清,您有两个选择:

1)在 ready() 外部声明变量,但然后在 ready() 内部定义变量:

var someVariable;
function callMe() {
  someVariable++;
  alert(someVariable);
}

$(document).ready(function() {
  someVariable = 3;
  callMe(); // Should display '4'
});

2) 在 内>ready(),使用 window.yourVariable = 'whatever'; 定义变量

Yes, you can do that, it's just a matter of scope.

If you only need to access callMe() from within $(document).ready(function() { }), then it's fine to put the function there, and offers some architecture benefits because you can't access the function outside of that context.

If you need to use the callMe() function outside of document ready though, you need to define the callMe() function outside of that context.

function callMe() {
  // Do Something
}

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

UPDATE

Based on your clarification, you have two options:

1) DECLARE variable outside of ready(), but then define variable inside of ready():

var someVariable;
function callMe() {
  someVariable++;
  alert(someVariable);
}

$(document).ready(function() {
  someVariable = 3;
  callMe(); // Should display '4'
});

2) Within ready(), define variables using window.yourVariable = 'whatever';

假情假意假温柔 2024-12-02 09:31:39

这也会起作用。

$(document).ready(function() {
      callMe = function() {
            alert('hello');
      }
});

callMe();

如果您使用

 var callMe = function () { ... }

它可能不起作用,并且可能会收到错误“函数未定义”

This will also work.

$(document).ready(function() {
      callMe = function() {
            alert('hello');
      }
});

callMe();

If you use

 var callMe = function () { ... }

It may not work and you may get an error "function is undefined"

娇妻 2024-12-02 09:31:39

您可以这样做:

$(document).ready(function(){

    var callMe = function(){
       //enter code here
    }

    $(".my-class").on("click", function(){
       callMe();
    });

});

因此,您不需要将函数放在准备好的文档之外,并且代码会变得分组且更有组织。 ;)

You can do like that:

$(document).ready(function(){

    var callMe = function(){
       //enter code here
    }

    $(".my-class").on("click", function(){
       callMe();
    });

});

So, you don't need to put the function outside the document ready and the code becomes grouped and more organized. ;)

心房的律动 2024-12-02 09:31:39

直接调用该函数可能是一个更好的主意,如下所示:

$(document).ready(myFunction);

function myFunction() {

   // Your code here

}

It is probably a better idea to call the function directly like so:

$(document).ready(myFunction);

function myFunction() {

   // Your code here

}
愁以何悠 2024-12-02 09:31:39

当您在 $(document).ready 中创建函数时,可以保证在文档加载之前不会调用该函数。当然,它只能从该事件处理程序本身(事件处理程序稍后的某个位置)调用。

换句话说,你想要做的事情是有效的(尽管不一定是可取的——你必须透露更多关于你想要完成的事情)。

When you create a function inside $(document).ready, it's guaranteed that it won't be called before the document has loaded. Of course, it can only be called from that event handler itself (somewhere later in the event handler).

In other words, what you're trying to do is valid (though not necessarily desirable - you'd have to reveal more about what you are trying to accomplish).

灼疼热情 2024-12-02 09:31:39

这绝对是合法的。问题是你为什么要这么做?可能是将函数的范围绑定到ready的范围,而不是将其全局绑定到window对象。但这是你真正想要的吗?我建议看看 javascript 中的函数闭包以及它如何处理作用域。帮助澄清它的需要......

This is definitely legal. The question is why do you want to do it? Probably to bind the function's scope to that of ready and not have it globally bound to the window object. But is that what you really want? I suggest having a look on function closures in javascript and how it handles scoping. to help clarify the need for it...

夜声 2024-12-02 09:31:39

看看是否可以通过将标记移动到 html 文件的底部来消除使用 document.ready 的需要。这应该会让事情变得简单很多。否则,在 document.ready 范围之外声明该函数,然后在 document.ready 函数范围内调用它。

See if you can eliminate the need for using document.ready by moving your tag to the bottom of the html file. This should make things alot simpler. Otherwise, declare the function outside the scope of the document.ready and just call it in the document.ready function scope.

蓝海 2024-12-02 09:31:39

您可以像下面这样使用:

$(document).ready(function () {
    printReport = function(rowIndex) {
        // Your code here
    }
});

您可以从任何事件中调用此函数。

You can use like the following:

$(document).ready(function () {
    printReport = function(rowIndex) {
        // Your code here
    }
});

You can call this function from any event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文