我有同樣的問題,這個解決方案需要一些jQuery。
我提供的例子是使用傳統鍵盤/鼠標的更復雜的例子。但對於觸摸設備,只需按照左鍵單擊部分即可。
- 左鍵點擊會彈出一個鏈接在同一個窗口
- 右鍵會彈出一個鏈接在新窗口中
- 左鍵單擊拖動來選擇允許複製和粘貼仍然(在我的方案中,桌面用戶將使用他們的複製/粘貼按鍵,而不是右鍵單擊)
不能100%確定Safari的具體細節,但這通常適用於所有現代瀏覽器。
這裏是一個鏈接表我想訪問:
<table>
<tr class="row"><td>http://google.com</td></tr>
<tr class="row"><td>http://teslamotors.com</td></tr>
<tr class="row"><td>http://solarcity.com</td></tr>
</table>
下面是使用jQuery的最新版本來處理mousedown
和腳本mouseup
結合:
<script>
$(document).ready(function() {
var lastMouseDownEvent = null;
// capture your last "mousedown" event and store it
$(".row").mousedown(function(event) {
console.log(event); // lets see the data in your Developer Mode log
lastMouseDownEvent = event;
});
// catch the "mouseup" event and compare the distance from "mousedown"
$(".row").mousedown(function(event) {
console.log(event);
var href = $(this).find("td").html();
switch(event.which) {
case 1: // left click
// only process left click if mousedown and mouseup occur at the same location on screen.
// NOTE: for my mom's shaky hand I added the 5 pixel allowance.
if (Math.abs(event.clientX-lastMouseDownEvent.clientX) < 5 &&
Math.abs(event.clientY-lastMouseDownEvent.clientY < 5))
window.location.href = href;
break;
case 2: // right click
window.open(href);
break;
}
lastMouseDownEvent = null;
});
});
</script>
這不是一個真正的錯誤我想想,但看看這個先前的問題,他們在addEventListener('touchstart')成功的地方; – Marcus 2012-01-31 23:42:12
@Marcus - 不幸的是,touchstart並不是一種有效的點擊方式,因爲它們的行爲不同 - 最大的擔憂是touchstart沒有區分點擊和滾動手勢,所以我仍然認爲這是一個錯誤。 – riscarrott 2012-02-01 11:07:31
如果您需要,您可以使用touchstart + touchmove + touchend,並在touchmove上設置swiping = true標誌,以便僅在點擊時運行您的事件。作爲一個側面說明,Apple文檔正式將複製/剪切事件列爲不受支持的,所以您是對的 - 這可能是一個沒有簡單修復的錯誤。如何綁定事件中使用非內聯見http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW1 – 2013-02-19 21:12:45