<table class="filetable" id="filetable">    
    <thead>
        <tr>
        <th width="25%">名称</th>
        <th width="6%">大小</th>
        <th width="10%">类型</th>
        <th width="19%">修改日期</th>
        <th width="40%"></th>
        </tr>        
    </thead>    
    <tbody id="FileList" runat="server">    
        <tr>
        <td><img src="images/f01.png" />Adobe Dreamweaver CS5简体中文绿色</td>
        <td>2013/10/14 17:38</td>
        <td>文件夹</td>
        <td class="tdlast">0 KB</td>
        <td></td>
        </tr>
    </tbody>
    </table>

this is a table, I want to get the value of each FileName when double click coresponding item.
I write script as follow:

<script>
    function Test() {
        var rows = document.getElementById("filetable").rows;
        if (rows.length > 0) {
            for (var i = 1; i < rows.length; i++) {
                (function (i) {
                    var temp = rows[i].cells[0].childNodes[1];


                    var obj = rows[i];
                    obj.ondblclick = function () { alert(temp); };
                })(i)
            }
        }
    }

    window.onload = function () { Test(); }
</script>

the output of alert is [object Text].
So , how can I get the value instead of type

Recommended Answers

All 4 Replies

While I don't agree with your use of an anonymous function to attach an anonymous function to a node to alert the value (... yup, that sounds right), it seems that you are trying to get the text value of the first node. So, you are looking for the innerText(for those browsers that support it), and a fallback of innerHTML. The below should catch that for you. Not totally sure that syntax will not error out on browsers that don't support innerText, so you may want to clean it up some.

<script>
    function Test() {
        var rows = document.getElementById("filetable").rows;
        if (rows.length > 0) {
            for (var i = 1; i < rows.length; i++) {
                (function (i) {
                    var temp = rows[i].cells[0].childNodes[1];


                    var obj = rows[i];
                    obj.ondblclick = function () { alert(temp.innerText || temp.innerHTML); };
                })(i)
            }
        }
    }

    window.onload = function () { Test(); }
</script>

Thanks for your reply.Maybe my description is not clear.

Well, for the HTML page, I want to get "Adobe Dreamweaver CS5??????" instead of "<img src="images/f01.png" />Adobe Dreamweaver CS5??????"
I got problems in locating it.
I tried the solution you post , it doesn't work as well.

innerHTML will give you the HTML inside the node, if you want plain text use .innerText;

Thnak you AleMonteiro and ryantroop! I figure it out.

to locate the <td> element , I use

rows[i].cells[0]

to output the filename without <img> element , I use

temp.innerText

problem tackled!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.