Onclick属性不适用于链接(a href)

时间:2012-09-19 08:45:44

标签: javascript android html cordova

我正在开发一个带有phonegap的Android应用程序,我有一个包含一些数据的表。单击一行时,它应返回索引并触发一个函数,因此我将每一行转换为带有a href属性的html onclick。问题是href它正在工作,它会返回到索引但是ti不会触发该函数。这是生成表的代码和函数的代码:

function doLocalStorage(xyz) {
        alert(xyz);
    }

<table id="hor-minimalist-b"><thead></thead><tbody></tbody></table>
    <script language="javascript">
    var table = document.getElementById('hor-minimalist-b'); // get the table element
    var tableBody = table.childNodes[1]; // get the table body
    var tableHead = table.childNodes[0]; // get the table head
    var thead = document.createElement('th');
    var row2 = document.createElement('tr'); // create a new row
    var headText = document.createTextNode('Dados');
      thead.scope = "col";
        thead.appendChild(headText);
        row2.appendChild(thead);
        tableHead.appendChild(row2);

    for (var i=0; i<len; i++) {

        var row = document.createElement('tr'); // create a new row

        var cell = document.createElement('td'); // create a new cell
        var a = document.createElement('a');
        var cellText = document.createTextNode(localStorage.getItem('key' + i));
        var xyz = localStorage.key(i);
        a.href = "index.html";
        a.onclick = "doLocalStorage(xyz);";
        a.appendChild(cellText);
        cell.appendChild(a); // append input to the new cell
        row.appendChild(cell); // append the new cell to the new row
        tableBody.appendChild(row); // append the row to table body
        }
    </script>

谢谢:)

2 个答案:

答案 0 :(得分:1)

你应该真正使用jQuery,因为它会解决你的浏览器不一致问题。

二,你可能需要像这样使用onlick。

a.onclick = function() { doLocalStorage(xyz); };

答案 1 :(得分:0)

尝试删除href属性并使用重定向window.location.href = ('url')处理单击事件。它应该有所帮助。