简体   繁体   English

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

[英]Onclick property not working for link (a href)

I'm developing an android app with phonegap and I have a table with some data. 我正在开发带phonegap的android应用,并且我有一个包含一些数据的表。 When clicking on a row it should go back to index and fire a function, so I turn each row into an html a href with a onclick property. 单击一行时,它应该返回索引并触发一个函数,因此我将每一行变成具有onclick属性的html a href The problem is that the href it's working and it goes back to the index but ti doesn't fire the function. 问题是href正常工作,并且可以返回索引,但是ti不会触发该函数。 Here's the code that generates the table and the code of the function: 这是生成表的代码和该函数的代码:

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>

Thanks :) 谢谢 :)

One you should really be using jQuery as it will take care of browser inconsistencies for you. 您确实应该使用jQuery,因为它将为您解决浏览器的不一致问题。

Two, you'll likely need to use onlick like this. 第二,您可能需要像这样使用onlick。

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

Try to delete a href attribute and handle a click event with redirection window.location.href = ('url') . 尝试删除href属性并使用重定向window.location.href = ('url')处理click事件。 It should help. 应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM