onclick有效,但我也想要一个视觉效果

时间:2014-07-04 07:03:55

标签: javascript android html

在Android应用程序中,我有一个tr onclick =" HitRow(这个,' http://www.test.com');" ...单击整个表行时转到另一个URL。这很好。

    function HitRow(cell,loc) 
        { 
        window.location.href = loc;
        cell.style.backgroundColor = "red";
        }

但是我也希望在行上进行视觉点击。我尝试使用cell.style.backgroundColor设置背景颜色,但这在用户导航后和/或它以一种令人困惑的方式瞬间闪烁时起作用。

我理想的是要在触摸时更改颜色,并触摸要执行的href。可以这样做吗?

由于

2 个答案:

答案 0 :(得分:1)

我会使用不引人注目的Javascript,touch eventsdata attributes来执行此操作:

<强>标记:

<tr data-url="http://www.google.com">

</tr>

<强>使用Javascript:

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");
}

答案 1 :(得分:0)

您可以尝试:

而不是:

function HitRow(cell,loc) 
            { 
            window.location.href = loc;
            cell.style.backgroundColor = "red";
            }

试试这个:

function HitRow(cell,loc) 
        { 
        cell.style.backgroundColor = "red";
        window.location.href = loc;

        }