简体   繁体   English

Javascript变量作用域和链接函数调用

[英]Javascript variable scoping and chained function calls

Given the following Javascript (assume jquery 2.1.1) 给定以下Javascript(假设jquery 2.1.1)

var Bar = [
  {
    name: "Bob",
    id: "bb1019",
    roles: [
      "admin", "user"
    ]
  },
  {
    name: "Sally",
    id: "sa0378",
    roles: [
      "user"
    ]
  }
]

$(document).ready(function () {
  Bar.forEach(function (element, idx, array) {
    $("#foo").append($("<div>")
      .attr("class", "userBox")
      .append("Name: " + element.name + ", ID: " + element.id)
      .append($("<div>") // want to create jquery object on this div
        .attr("class", "rolesBox")
        .append(function () {
          element.roles.forEach(function (element, idx, array) {
            // How to create jquery object on closest div here
          }); 
        })
      )
    )
  });
}); 

I don't know if it's just too late in the evening for me or what, but I can't figure out how to create a jQuery object wrapping the DOM element I want. 我不知道对于我来说还是太晚了,但是我不知道如何创建一个包装我想要的DOM元素的jQuery对象。 I've commented where I want to create the jQuery and the element I want to wrap. 我已在要创建jQuery的位置以及要包装的元素上发表了评论。

I tried this thinking this was the right path but it's bound to the global object. 我想this想这是正确的道路,但它绑定到全局对象。 Clearly not what I want. 显然不是我想要的。

To work with any object in javascript out of its scope (jQuery included) you need to have a reference to it, if you put the code all inline you have no reference to it anywhere else. 要在javascript范围之外(包括jQuery)使用任何对象,都需要对其进行引用,如果将代码全部内联,则在其他任何地方都没有引用。

For example, you can create your component parts and then append them. 例如,您可以创建您的零部件,然后附加它们。

$(document).ready(function () {
    Bar.forEach(function (element, idx, array) {
        // Components
        var $user = $("<div />", { "class" : "userBox" }).append(
                "Name: " + element.name + ", ID: " + element.id
            ),

            $box = $("<div />", { "class" : "rolesBox" });

        // Stitch it together and append to DOM 
        $("#foo").append(
            $user.append(
                $box.append(function () {
                    element.roles.forEach(function (element, idx, array) {
                        // $box and $user variables available for you here
                    }); 
                })
            )
        );
    });
});

You can capture the div inside the append function as follows: 您可以按如下所示在append函数中捕获div:

.append($("<div>") // want to create jquery object on this div
        .attr("class", "rolesBox")
        .append(function () {
            var $div = $(this);

            element.roles.forEach(function (element, idx, array) {
                $div.append(element + ' ');
            });                  
        })

Fiddle 小提琴

Try var $divelement = $('<div>') 尝试var $divelement = $('<div>')

$(document).ready(function () {
    Bar.forEach(function (element, idx, array) {
        var $divElement = $("<div>", {
            "class": "rolesBox"
        });
        $("#foo").append($("<div>")
            .attr("class", "userBox")
            .append("Name: " + element.name + ", ID: " + element.id)
            .append($divElement
            .append(function () {
            element.roles.forEach(function (element, idx, array) {
                // How to create jquery object on closest div here
                $divElement.append('<div>' + element + '</div>')
            });
        })));
    });
});

JS Fiddle JS小提琴

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

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