动态添加对href的描述

时间:2015-12-13 06:34:03

标签: javascript html hyperlink

我有一个表,在从文件读取数据后动态添加记录。 第一列是标签,第二列是复选框。当我添加<a>标记时,它会添加到表中而没有任何可点击的链接。 如何添加链接?

HTML

<html>
<body>
<div id="page-wrapper">
    <form onsubmit="download(this['dataTbl'].value)">
        <h1>Text File Reader</h1>
        <div>
            Select a text file: 
            <input type="file" id="fileInput">
        </div>
        <pre id="fileDisplayArea"><pre>
        <div id='tableDiv'>
            <table id ="dataTbl" width="320" border="1">

            </table>
        </div>
        <button id="submitLink">Save Data</button>
    </form>
</div>

</body>
</html>

JS

function addTable(result) 
{
    var splitted = result.split("\n");
    for (i = 0; i < splitted.length; i++)
    {
        filename=splitted[i]+".mp4"
        //var link = '<tr><td><a href="Y:/MaxTopics/Videos/'+filename+'">'+filename+'</a></td><td><input type="checkbox" id ="chk'+filename+'"></input></td></tr>';
        var tbl = document.getElementById('dataTbl');
        //create row
        var row = document.createElement('tr');
        //create column
        var col1 = document.createElement('td');
        //create link
        var movieLink = document.createElement('a');
        //set attribute for movieLink
        movieLink.setAttribute("href", "Y:/MaxTopics/Videos/"+filename);
        //append movieLink for col1
        col1.appendChild(movieLink);
        // append column for row
        var span = document.createElement('span')
        s
        row.appendChild(col1);
        var col2 = document.createElement('td');
        var chk = document.createElement('input');
        chk.setAttribute("type", "checkbox");
        chk.setAttribute("id", "checkbox"+filename);
        col2.appendChild(chk);
        row.appendChild(col2);
        row.setAttribute("id","test");
        //append row to table
        tbl.appendChild(row);
    }

}

1 个答案:

答案 0 :(得分:1)

您似乎忘了设置链接的内容:

movieLink.innerHTML = filename;

空链接不会占用任何空间,因此不可见或无法点击:

&#13;
&#13;
<a href="#foo"></a>
&#13;
&#13;
&#13;