修复不正确的嵌套

时间:2009-12-24 04:25:04

标签: javascript html css function

function drawinventoryList() {
    inventoryArray.sort();
    var inventoryString = "";
    for (x in inventoryArray) {
        arrayValue = inventoryArray[x];
        var counter = parseInt(x) + 1;
        if (counter == 1) {
            inventoryString = "<table width='100%' celpadding='0' cellspacing='0' style='border:1px solid #d00;'>"
        }
        if (arrayValue.substring(0, arrayValue.indexOf("@")) != "XXXXXXXX") {
            inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("@")) + ", " + arrayValue.substring(arrayValue.indexOf("@") + 1) + " (<a href=\"javascript:removeinventory('" + x + "')\">remove</a>)</td></tr>";
        }
    }
    if (inventoryString == "") inventoryString = "None."
    document.getElementById("selectedInventories").innerHTML = inventoryString;
}

...此函数会创建一个无效的表。我需要在单独的列中使用“计数器”名称和“删除链接”。如何正确创建和关闭所有标签?

非常感谢

2 个答案:

答案 0 :(得分:1)

看起来你只是忘记了桌子的结束标签。试试这个:

function drawinventoryList() {
    inventoryArray.sort();
    var inventoryString = "";
    for (x in inventoryArray) {
        arrayValue = inventoryArray[x];
        var counter = parseInt(x) + 1;
        if (counter == 1) {
            inventoryString = "<table width='100%' celpadding='0' cellspacing='0' style='border:1px solid #d00;'>"
        }
        if (arrayValue.substring(0, arrayValue.indexOf("@")) != "XXXXXXXX") {
            inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("@")) + ", " + arrayValue.substring(arrayValue.indexOf("@") + 1) + " (<a href=\"javascript:removeinventory('" + x + "')\">remove</a>)</td></tr>";
        }
    }
    if (inventoryString == "") {
        inventoryString = "None.";
    } else {
        inventoryString = inventoryString + "</table>";
    }
    document.getElementById("selectedInventories").innerHTML = inventoryString;
}

唯一的变化来自:

if (inventoryString == "") inventoryString = "None."

为:

    if (inventoryString == "") {
        inventoryString = "None.";
    } else {
        inventoryString = inventoryString + "</table>";
    }

更新:在评论中,您说明了

  

我仍然需要在分开的[原文如此] TD

中分隔计数器,名称和链接

这可能只是插入一些</td><td>部分,例如:

inventoryString = inventoryString +
    "<tr>" +
        "<td>" + counter + ".</td>" + 
        "<td>" + arrayValue.substring(0, arrayValue.indexOf("@")) + ", " +
            arrayValue.substring(arrayValue.indexOf("@") + 1) + "</td>" +
        "<td>(<a href=\"javascript:removeinventory('" + x +
            "')\">remove</a>)</td>" +
    "</tr>";

答案 1 :(得分:1)

我不完全确定你要做什么,但也许这可能就是你想要的。

function drawinventoryList() {
    inventoryArray.sort();
    var inventoryString = "<table width='100%' celpadding='0' cellspacing='0'   style='border:1px solid #d00;'>";
    var count = 0;
    for (x in inventoryArray) {
        arrayValue = inventoryArray[x];            
        count++;
        if (arrayValue.substring(0, arrayValue.indexOf("@")) != "XXXXXXXX") {
            inventoryString = inventoryString + "<tr><td>" + counter + ". " + arrayValue.substring(0, arrayValue.indexOf("@")) + ", " + arrayValue.substring(arrayValue.indexOf("@") + 1) + " (<a href=\"javascript:removeinventory('" + x + "')\">remove</a>)</td></tr>";
        }
    }

    if (count == 0) {
        inventoryString = "None.";
    } else {
        inventoryString = inventoryString + "</table>";
    }
    document.getElementById("selectedInventories").innerHTML = inventoryString;
}