我尝试在用户触摸/轻按手机时更改链接的背景颜色,尽管以下代码有效,但有时链接保持黑色并且在您释放时不会变回白色,有没有更好的方法编码吗?
$('#test').bind('touchstart', function() {
$(this).css('background-color', 'black');
});
$('#test').bind('touchend', function() {
$(this).css('background-color', 'white');
});
谢谢!
最佳答案
这可以在没有jQuery的CSS中完成:
a:active
{
background-color: black;
}
关于与Android的常规浏览器兼容的注意事项由于某些原因,以上代码在Android的常规浏览器(可能还有其他一些浏览器)中不起作用。但是,将
ontouchstart=""
参数添加到<body>
标记后,它将可以正常工作,如下所示:<body ontouchstart="">
另外,Android股票浏览器默认情况下突出显示链接(我手机上的背景为蓝色)。要禁用此功能,请键入:a
{
-webkit-tap-highlight-color: rgba(255, 255, 255, 0);
}
有关更多详细信息,请参见this和this。Jsfiddle here