为什么我的元素不被视为等同于自身?

B. *_*non 0 html c# jquery blur sharepoint-2010

我在C#中创建了这样的html输入文本元素:

boxIndex1 = new TextBox()
{
    CssClass = "dplatypus-webform-field-input indexcell",
    Width = TEXTBOX_WIDTH,
    ID = "boxIndex1foapalrow2"
};
Run Code Online (Sandbox Code Playgroud)

...以及此jQuery响应“ boxIndex1foapalrow2”及其表亲(“ boxIndex2foapalrow3”,“ boxIndex3foapalrow4”等)的模糊事件:

$(document).on("blur", '.indexcell', function (e) {
    var $currentindexcell = $(this);
    if ($currentindexcell == $('[id$=boxIndex1foapalrow2]')) {
        alert('index cell 1 exited'); // testing
    }
});
Run Code Online (Sandbox Code Playgroud)

我逐步完成了它,当我从“ boxIndex1foapalrow2”中跳出时分配给$ currentindexcell的元素似乎是我所期望的:

<input name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$boxIndex1foapalrow2" type="text" id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_boxIndex1foapalrow2" class="dplatypus-webform-field-input indexcell" style="width:88px;">
Run Code Online (Sandbox Code Playgroud)

...但警报未显示/如果条件等于假。为什么?在我看来$currentindexcell,这种情况下的值确实相等$('[id$=boxIndex1foapalrow2]'),但是为什么对Javascript执行引擎却不这样呢?

gil*_*ly3 5

包含相同元素集的两个jQuery对象不相等。要测试您的jQuery对象是否与选择器匹配,请使用.is()

if ($currentindexcell.is('[id$=boxIndex1foapalrow2]')) {
Run Code Online (Sandbox Code Playgroud)

如果您真的想检查是否相等,则应该比较实际的元素(而不是保存它们的jQuery对象):

if (this == $('[id$=boxIndex1foapalrow2]')[0]) {
Run Code Online (Sandbox Code Playgroud)