2012-09-19 29 views
1

我正在开发一个带有phonegap的android应用程序,我有一个包含一些数据的表。当点击一行时,它应该返回到索引并激活一个函数,所以我将每行变成一个具有onclick属性的html a href。问题是href它的工作,它回到索引,但ti不会触发该功能。下面是生成的表和函数的代码的代码:Onclick属性不能用于链接(a href)

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> 

谢谢:)

回答

1

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

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

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

谢谢,它工作。你能解释一下在这种情况下如何使用jQuery吗?我对Android和JavaScript有点新鲜。提前致谢。 –

+0

您可以在线获取jQuery网站或Google的许多教程。那里有很多初学者教程。使用localstorage可能会很好,只是直接使用它,但对于处理事件,遍历DOM和DOM操作,jQuery是一个很大的帮助。 – FluffyJack

0

尝试删除href属性和处理click事件与重定向window.location.href = ('url')。它应该有所帮助。