Javascript <;tr>;onclick可以工作,但我也想要一个视觉效果
在android应用程序中,我有一个tr onclick=“HitRow(this)”;“。。。单击整个表行时转到另一个url。这个很好用Javascript <;tr>;onclick可以工作,但我也想要一个视觉效果,javascript,android,html,Javascript,Android,Html,在android应用程序中,我有一个tr onclick=“HitRow(this)”;“。。。单击整个表行时转到另一个url。这个很好用 function HitRow(cell,loc) { window.location.href = loc; cell.style.backgroundColor = "red"; } 但是,我还希望在该行上进行可视化单击。我试着用cell.style.backgroundCol
function HitRow(cell,loc)
{
window.location.href = loc;
cell.style.backgroundColor = "red";
}
但是,我还希望在该行上进行可视化单击。我试着用cell.style.backgroundColor设置背景色,但在用户导航离开和/或它以令人困惑的方式瞬间闪烁后,这种设置就起作用了
理想情况下,我希望在触摸时更改颜色,在触摸时执行href。这能做到吗
谢谢您可以尝试:
与此相反:
function HitRow(cell,loc)
{
window.location.href = loc;
cell.style.backgroundColor = "red";
}
试试这个:
function HitRow(cell,loc)
{
cell.style.backgroundColor = "red";
window.location.href = loc;
}
我会使用不引人注目的Javascript,为此: 标记:
<tr data-url="http://www.google.com">
</tr>
var tr = document.querySelector("tr");
tr.addEventListener("touchstart", function() {
this.style.backgroundColor = "red";
});
tr.addEventListener("touchend", function() {
window.location.href = this.getAttribute("data-url");
});
如果您有多个tr
元素,则需要使用document.querySelectorAll
并循环遍历它们:
var i;
var elements = document.querySelectorAll("tr[data-url]");
for(i=elements.length;i--) {
var element = elements[i];
element.addEventListener("touchstart", doTouchStart);
element.addEventListener("touchend", doTouchEnd);
}
function doTouchStart() {
this.style.backgroundColor = "red";
}
function doTouchEnd() {
window.location.href = this.getAttribute("data-url");
}