获取Javascript中的表

获取Javascript中的表,javascript,html,Javascript,Html,我有一张桌子: <div ID="documents_list" style="visibility: hidden;"> <table style="background-color:#000000;border:0;border-collapse:separate;border-spacing:0px;"> <tr> <td style="padding:0px;">

我有一张桌子:

<div ID="documents_list" style="visibility: hidden;">
    <table style="background-color:#000000;border:0;border-collapse:separate;border-spacing:0px;">
        <tr>
            <td style="padding:0px;">
                <table id="documentsTable" style="border:0;border-collapse:separate;border-spacing:1px;">
                    <tr bgcolor='#ffffff'>
                        <td bgcolor='#cccccc' style="padding:2px;" nowrap>Catalog:</td>
                        <td style="padding:2px;" colspan=3></td>
                    </tr>
                    <tr bgcolor='#ffffff'>
                        <td bgcolor='#cccccc' style="padding:2px;" nowrap>Doc.nr.</td>
                        <td bgcolor='#cccccc' style="padding:2px;" nowrap>Document name</td>
                        <td bgcolor='#cccccc' style="padding:2px;" nowrap></td>
                        <td bgcolor='#cccccc' style="padding:2px;" nowrap></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <input type="button" value="Close" onClick="hide_documents_list()">
</div>

目录:
文件编号:。
文件名
我试图从Javascript访问它,这样我就可以更改它的内容并向其中添加行。其方法如下:

function show_documents(documents) {
    var table = document.getElementById("documentsTable");
    for (var i=0; i<documents.length; i++) {
        var row = table.insertRow(-1);

        var document = row.insertCell(0);
        var name = row.insertCell(1);
        var button = row.insertCell(2);
        var checkbox = row.insertCell(3);

        document.innerHTML = documents[i].document;
        name.innerHTML = "<a href='${pageContext.request.contextPath}/s?id=" + documents[i].document + "'>" + documents[i].name + "</a>";
        button.innerHTML = "<input type='button' value='Delete'>";
        name.innerHTML = "<input type='checkbox'>";
    }
    show_documents_list();
}
功能显示文档(文档){
var table=document.getElementById(“documentsTable”);

对于(var i=0;i是的,就像Tero Tolonen说的那样。因为Javascript在执行时将所有变量声明拉到顶部

这意味着在代码执行方面,您会得到如下结果:

// because of hoisting variable decalarations get pulled to the top of the scope
var document;
然后尝试访问声明变量的方法
getElementByID


因此会导致类型错误。

浏览器中的文档是始终可用的全局对象。您不能单独使用您指定此名称的变量。

在JavaScript中,存在一个,因此所有变量声明(常规对象或函数,请注意JavaScript中的函数也是object类型)将移动到其功能范围的顶部

请注意,初始化不会被提升到顶部

例如,考虑这一点:

function(){
    alert("hi there!");
    var x = 5; // Declaration and initialization
    alert(x);
}
应用提升后的示例如下所示:

function(){
    var x; // Declaration is hoisted but not for initialization
    alert("hi there!");
    x = 5; // Initialization is still here.
    alert(x);
}
在你的情况下,我们会有这样的东西后,吊装应用

function show_documents(documents) {
    var table;
    var row;
    var document; // This variable is now of type Unknown
    var name;
    var button;
    var checkbox;

    table = document.getElementById("documentsTable");
    //         ^--- Unknown, But 'window.document' does work.
    for (var i=0; i<documents.length; i++) {
        row = table.insertRow(-1);

        document = row.insertCell(0); // Initialization is here!
        name = row.insertCell(1);
        button = row.insertCell(2);
        checkbox = row.insertCell(3);

        document.innerHTML = documents[i].document;
        name.innerHTML = "<a href='${pageContext.request.contextPath}/s?id=" + documents[i].document + "'>" + documents[i].name + "</a>";
        button.innerHTML = "<input type='button' value='Delete'>";
        name.innerHTML = "<input type='checkbox'>";
    }
    show_documents_list();
}

看起来您已经在函数中将文档定义为局部变量,请将其重命名为document=row.insertCell(0)中的其他内容;我在Javascript中重命名了文档变量,似乎解决了此问题。谢谢!
var x = 5;
function (){
    var x = 4; // same name
    alert(x); // result would be 4 not 5
}