gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:01:12 24 4
gpt4 key购买 nike

我正在开发一个带有 phonegap 的 Android 应用程序,我有一个包含一些数据的表格。当点击一行时,它应该返回到索引并触发一个函数,所以我将每一行变成一个带有 onclick 属性的 html a href。问题是 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>

谢谢:)

最佳答案

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

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

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

关于javascript - Onclick 属性不适用于链接 (a href),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com