[英]Prevent click event in <a> tags when scrolling in mobile
我正在建立一個網站http://alfstyle.com ,在手機中向下滾動時遇到問題。 標記中的click事件會意外觸發。 真令人沮喪。
我曾嘗試停用插件,jQuery,一些腳本等。但沒有任何效果。
我嘗試通過在此處找到的javascript代碼段阻止重定向: 意外停止touchend觸發鏈接
但這不起作用。
提前致謝!
通常,在移動網站上滾動瀏覽不會激活網頁上的鏈接。 之所以發生這種情況,是因為向頁面注入了其他代碼。 我找到了一部分代碼,這些代碼可以使window.location發生變化。
function toggleSubmenuDisplay() {
document.addEventListener('touchstart', function(event) {
if ( event.target.matches('a') ) {
var url = event.target.getAttribute( 'href' ) ? event.target.getAttribute( 'href' ) : '';
// If there’s a link, go to it on touchend
if ( '#' !== url && '' !== url ) {
window.location = url;
// Open submenu if url is #
}
...
}
當前代碼已附加到所有touchstart操作上。 在移動設備上觸摸時,將執行此處理程序,並且還將執行此部分:
if ( '#' !== url && '' !== url ) {
window.location = url;
}
如果將調試器放在此處,您將看到此代碼已執行。
還要刪除此腳本,它會導致在觸摸手勢上重定向:
$('a').on('click touchend', function(e) {
var el = $(this);
var link = el.attr('href');
if (link !== undefined && link !== '') {
window.location = link;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.