$(document).ready是否在外部函数中调用? [英] $(document).ready called inside an external function?

查看:423
本文介绍了$(document).ready是否在外部函数中调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上某处看到了一些jquery代码,它采用这种形式:

I saw some jquery code on the Net somewhere, that took this form :

<script>            
function doSomething(message)
{   
    $(document).ready(function(){       
        alert(message);
        });
};
</script>

即外部函数("doSomething"),内部具有$(document).ready.我很困惑,因为加载DOM时不会触发$(document).ready下的代码吗?就像在函数(?)中包含事件处理程序一样.这种形式的代码对任何人都有意义吗?谢谢.

i.e. an external function ("doSomething") which has $(document).ready inside it. I'm confused because isn't the code under $(document).ready triggered when the DOM is loaded ? Its like having an event handler inside a function (?). Does this form of code make sense to anyone ? Thanks.

推荐答案

原始编码器可能存在错误.您可以做到这一点,但您想尽早致电doSomething.请注意,如果在调用ready时DOM已经准备就绪,则jQuery将调用该代码,因此将以任何一种方式发生,但是直到有人在某个地方调用doSomething时,它才会发生.我看不出该模式有多少用途,因此除非您有强烈的理由使用它并给出了问题,我怀疑您不是:-)—您可以放心地忽略它.

It was probably an error on the part of the original coder. You could do that, but you'd want to call doSomething pretty early on. Note that if the DOM is already ready when you call ready, jQuery will call the code, so it will happen either way, but it won't happen until someone, somewhere, calls doSomething. I can't see much purpose to the pattern, so unless you have some strong argument for using it — and given the question, I suspect you don't :-) — you can safely ignore it.

这篇关于$(document).ready是否在外部函数中调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆