为什么在函数中嵌套jQuery调用?

时间:2016-10-09 21:49:32

标签: javascript jquery

以下之间的区别(如果有):

$(document).ready(function() {
    $(function() {
        $("#selector").doSomething();
    });
});

$(document).ready(function() {
    $("#selector").doSomething();
});

我问,因为我通常会使用第二位代码,但jQuery Mobile网站提供了第一段代码作为示例。

3 个答案:

答案 0 :(得分:-1)

$(function() { ... })所有$(document).ready(function() { ... })都是import pandas as pd x = pd.DataFrame({0: [1,2,3], 1: [4,5,6], 2: [7,8,9] }) y = pd.Series([-1, 1, -1]) 的简称,因此您无需像在第一个代码段中那样使用它们而且是多余的。

答案 1 :(得分:-1)

根据jQuery documentation here

.ready()方法通常与匿名函数一起使用:

compgen

这相当于调用的推荐方式

$( document ).ready(function() {
  // Handler for .ready() called.
});

答案 2 :(得分:-2)

$(document).ready(function() {
    $(function() {
        var a = 5;
    });
    alert(a); //Outputs an is undefined error
});

与:

相比
$(document).ready(function() {
    var a = 5;
    alert(a); //Shows an alert with the number 5
});

在第一个代码示例aundefineda,因为它在不同的范围内,在第二个代码示例中5- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UIViewController alloc] init]; self.window.rootViewController.view = [[UIView alloc] init]; self.window.rootViewController.view.backgroundColor = [UIColor redColor]; return TRUE; } 因为它是&#39 ; s在同一范围内。

OP中的两个示例都具有相同的结果,但在定义的范围上有所不同,这导致上面的示例在不同的结果中但在其他情况下(如OP)在相同的结果中取决于代码在范围内。

相关问题