如何从 JQuery 块外部调用 JQuery

发布于 2024-08-04 00:28:36 字数 1035 浏览 3 评论 0原文

好吧,问题确实没有意义。情况是这样的。我有使用“Anthem”ajax 的遗留代码,Anthem 允许挂钩到回调前和回调后步骤。因此,如果我有这样的代码,如何在 cmdSave_PostCallBack 中的 .each 块下执行代码。我知道我不能按原样调用它,但是有什么方法可以允许在加载 doc 时以及在 postcallback 函数中执行此块?

 jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    })
    //display currently selected value
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;
});

function cmdSave_PostCallBack() {

}

Ok, question does not really make sense. Here is the situation. I have legacy code using "Anthem" ajax, and Anthem allows to hook into pre and post callback steps. So if I have code like this, how do I execute code under .each block in cmdSave_PostCallBack. I know I cannot call it the way it is, but what would be a way that would allow this block to be executed when doc is loaded and also in the postcallback function?

 jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    })
    //display currently selected value
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;
});

function cmdSave_PostCallBack() {

}

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

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

发布评论

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

评论(2

走过海棠暮 2024-08-11 00:28:36

创建另一个函数(我们将其称为驴),并在每个中使用它:(

抱歉,格式有点混乱)

...
.each(donkey);

function cmdSave_PostCallBack() {
    donkey(1);
}

function donkey(i) {
    var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
    if (selectionText.substr(0, 4) == "All ") {
        this.innerHTML += ": All";
    }
    else {
        this.innerHTML = this.innerHTML + ": " + selectionText;
    }
    });
return false;
}

Create another function (we shall call it donkey), and use that in the each:

(sorry, the formatting is a little messed up)

...
.each(donkey);

function cmdSave_PostCallBack() {
    donkey(1);
}

function donkey(i) {
    var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
    if (selectionText.substr(0, 4) == "All ") {
        this.innerHTML += ": All";
    }
    else {
        this.innerHTML = this.innerHTML + ": " + selectionText;
    }
    });
return false;
}
童话 2024-08-11 00:28:36

是不是就像将 .each 剥离到自己的函数中并从 Ready 和 cmdSave_PostCallBack 中调用它一样简单?

像这样的东西:

jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    });
    displayCurrentlySelectedValues();
    return false;
});

function cmdSave_PostCallBack() {
    displayCurrentlySelectedValues();
}

function displayCurrentlySelectedValues() {
    //Note how this uses the same selector as in .ready
    $("#<%=divSelectorLinks.ClientID %> a")
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;

}

Would it be as simple as ripping the .each out into its own function and calling it both from the ready and the cmdSave_PostCallBack?

Something like:

jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    });
    displayCurrentlySelectedValues();
    return false;
});

function cmdSave_PostCallBack() {
    displayCurrentlySelectedValues();
}

function displayCurrentlySelectedValues() {
    //Note how this uses the same selector as in .ready
    $("#<%=divSelectorLinks.ClientID %> a")
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;

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