0
<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

3
Contributors
4
Replies
14
Views
2 Years
Discussion Span
Last Post by tiredoy
0

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>

Edited by ryantroop

0

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.

0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.