0

我有以下内容,您可以看到我正在尝试this通过该函数在 JavaScript\jQuery 中是否有可能,如果可以的话,如何?似乎找不到任何我认为我的术语错误的东西。

function pageLoad(sender, args) {
    if (args.get_isPartialLoad()) {
        jQuery(".ShowPleaseWait").click(function () {
            processingReplacer("Please Wait...", this);
        });
        jQuery(".ShowProcessing").click(function () {
            processingReplacer("Processing...", this);
        });
    }
}

function processingReplacer(message, this) { 
        if (Page_IsValid) {
            jQuery(this).hide();
            jQuery(this).after("<img id='" + jQuery(this).attr('id') + "' class='" + jQuery(this).attr('class') + "' src='/content/images/processing.gif' /> " + message);
            alert("woohoo"); 
        }
}
4

2 回答 2

5

不能this用作函数参数的名称。将其更改为其他内容:

function processingReplacer(message, target) { 
    if (Page_IsValid) {
        jQuery(target).hide();
        jQuery(target).after("<img id='" + jQuery(target).attr('id') + "' class='" +
           jQuery(target).attr('class') + "' src='/content/images/processing.gif' /> " +
           message);
        alert("woohoo"); 
    }
}
于 2012-06-29T13:29:47.957 回答
2

你可以这样做:

processingReplacer.call(this, "Please Wait...");

...

function processingReplacer(message) { 
        if (Page_IsValid) {
            jQuery(this).hide();
            jQuery(this).after("<img id='" + jQuery(this).attr('id') + "' class='" + jQuery(this).attr('class') + "' src='/content/images/processing.gif' /> " + message);
            alert("woohoo"); 
        }
}

使用call您可以设置this. 然而,乔恩的答案可能更具可读性。

于 2012-06-29T13:31:12.790 回答