Jquery .each() 范围问题(bug?)

发布于 2024-12-02 04:17:29 字数 2636 浏览 0 评论 0原文

由于某种原因,我对嵌套 jQuery.each() 函数的调用失去了某些变量的范围,但其他变量却没有。在下面的代码中,Client.KNE 引用有效,但 ClientDiv 无效,即使在此之前,两者都是定义的填充变量...

通过将 Client 和 ClientDiv 切换为全局变量,它可以工作,但我觉得我不应该在这里创建全局变量...

不起作用:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            var KNE = Client.KNE[kneIndex];                       // Works
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // DOES NOT WORK

起作用:

jQuery.each(Messages.Additions, function (clientIndex) {
    Client = Messages.Additions[clientIndex];
    ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            KNE = Client.KNE[kneIndex];                       // Works
            KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // Works

有人知道我在第一个版本中做错了什么吗?或者这是一个错误?为什么一个变量起作用,而另一个变量不起作用...

从这里: Jquery $ ().each 方法掩盖了 'this' 关键字 看起来我可以将变量传递到函数调用中,但我必须这样做吗?

尝试了上面的链接,但它不起作用:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex, Client, ClientDiv) {
            var KNE = Client.KNE[kneIndex];
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);   //Does not work - ClientDiv undefined

类似的问题没有令人满意的答案: jQueryeach() 函数的范围?

解决方案

$.each(Messages.Additions, function () {
    var $Client = this;
    var $ClientDiv = $("#clientTitle_" + $Client.ClientID);

    if (!$ClientDiv.length) {
        $("#ClientTemplate").tmpl($Client).appendTo("#ClientContainer");
    } else {
        $.each($Client.KNE, function () {
            var $KNE = this;
            var $KNEDiv = $ClientDiv.find("#kneTitle_" + jq($KNE.KNE));    
            // SWITCHED TO $ PREFIX

For some reason my call to nested jQuery.each() functions are losing scope for some variables, but not others. In the code below, the Client.KNE reference works, but ClientDiv does not, even though prior to that each, both are defined, populated variables...

By switching Client and ClientDiv to global variables, it works, but I feel like I should not have to create global variables here...

Doesn't Work:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            var KNE = Client.KNE[kneIndex];                       // Works
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // DOES NOT WORK

Does Work:

jQuery.each(Messages.Additions, function (clientIndex) {
    Client = Messages.Additions[clientIndex];
    ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex) {
            KNE = Client.KNE[kneIndex];                       // Works
            KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);  // Works

Anyone know what I'm doing wrong in the first version? Or is this a bug? Why does the one variable work but the other doesn't...

From here: Jquery $().each method obscures 'this' keyword it looks like I could pass the variables into the function call, but should I have to?

Tried the above link, and it is not working:

jQuery.each(Messages.Additions, function (clientIndex) {
    var Client = Messages.Additions[clientIndex];
    var ClientDiv = $("#clientTitle_" + Client.ClientID);

    if (ClientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function (kneIndex, Client, ClientDiv) {
            var KNE = Client.KNE[kneIndex];
            var KNEDiv = ClientDiv.find("#kneTitle_" + KNE.KNE);   //Does not work - ClientDiv undefined

Similar questions without satisfactory answer:
Scope of jQuery each() function?

SOLUTION

$.each(Messages.Additions, function () {
    var $Client = this;
    var $ClientDiv = $("#clientTitle_" + $Client.ClientID);

    if (!$ClientDiv.length) {
        $("#ClientTemplate").tmpl($Client).appendTo("#ClientContainer");
    } else {
        $.each($Client.KNE, function () {
            var $KNE = this;
            var $KNEDiv = $ClientDiv.find("#kneTitle_" + jq($KNE.KNE));    
            // SWITCHED TO $ PREFIX

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

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

发布评论

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

评论(1

自演自醉 2024-12-09 04:17:29

您可以使用指向循环中当前项目的 this 关键字来尝试此操作。您应该检查 if (ClientDiv.length > 0),而不是检查 if (ClientDiv == null) 因为 jQuery 返回 am 空如果没有找到该元素,则检查将失败。

var additions;
jQuery.each(Messages.Additions, function () {
    var $clientDiv = $("#clientTitle_" + this.ClientID);

    if ($clientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function () {
            $clientDiv.find("#kneTitle_" + this.KNE);
        });
    }
});

You can try this using this keyword which points to the current item in the loop. Instead of checking for if (ClientDiv == null) you should check for if (ClientDiv.length > 0) because jQuery returns am empty object if it do not finds the element so that check will fail.

var additions;
jQuery.each(Messages.Additions, function () {
    var $clientDiv = $("#clientTitle_" + this.ClientID);

    if ($clientDiv.length == 0) {
        $("#ClientTemplate").tmpl(Client).appendTo("#ClientContainer");
    } else {
        jQuery.each(Client.KNE, function () {
            $clientDiv.find("#kneTitle_" + this.KNE);
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文