簡體   English   中英

Javascript關聯數組不適用於Jquery對象

[英]Javascript associative array not working on Jquery objects

我正在嘗試建立一個關聯數組parentTds但是它沒有按照我想要的方式工作。

var parentTds = {};
var index = 0;
$.each(clone, function () {
    var $currentItem = $(selectedActivities[index]);
    var $currentItemTd = $currentItem.closest('td');
    console.log($currentItemTd.get(0));
    var borderLeft = 0;
    console.log(parentTds[$currentItemTd.get(0)]);
    console.log(parentTds[$currentItemTd]);
    if (typeof parentTds[$currentItemTd.get(0)] === "undefined") {
        console.log('NOT OK');
        borderLeft++;
        parentTds[$currentItemTd.get(0)] = $currentItemTd;
    } else {
        console.log('OK');
    }
    index++;
});

由於某些原因, parentTds[$currentItemTd.get(0)]始終返回存儲的第一項。 我得到NOT OK僅僅是循環運行,而我應該得到的第一時間NOT OK了幾次。 我懷疑問題是parentTds[$currentItemTd.get(0)]本身。

有什么想法嗎?

Javascript不像PHP那樣寬容。 使用此功能時:

if (typeof parentTds[$currentItemTd.get(0)] === "undefined") {

$currentItemTd.get(0)可能被評估為0,因此引用始終是parentTds[0]

在這種情況下,我通常會拆分代碼塊並執行以下操作:

var cig = $currentItemTd.get(0);
if (typeof parentTds[cig] === "undefined") {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM