jquery dblclick在触摸设备上不起作用

k0pti3hp  于 5个月前  发布在  jQuery
关注(0)|答案(3)|浏览(59)

dblclick在触摸设备和PC浏览器上工作的最佳方法是什么
下面的代码与鼠标双击完全正常工作,但当在Android触摸设备上尝试时,不工作。我应该做些什么不同的?

$(document).on("dblclick","#table_discrepancy tr", function() {

 var orderno = $(this).find("td:eq(0)").text();
 var workorderno = $(this).find("td:eq(1)").text();

    server('/get_customer_info/' + orderno, function(result){

     var cus_name = result.name.replace(/^[\s]+/, '');
     cus_name = cus_name.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
     var phone_no = result.phoneno.replace(/^[\s]+/, '');
     var email = result.email.replace(/^[\s]+/, '');

          $('#customer_info_modal').modal('show');

          $('#orderno_modal').html('Order# : ' + orderno);
          $('#workorderno_modal').html('Work Order# : ' + workorderno);
          $('#customer_name_modal').html('Name : ' + cus_name);
          $('#customer_phoneno_modal').html('Phone#: ' + phone_no);
          $('#customer_email_modal').html('Email: ' + email);
    });
})

字符串

yshpjwxd

yshpjwxd1#

对于移动的,我会使用一个移动的特定事件,如taphold而不是双击,因为它可能会给给予更原生的感觉用户体验。
您可以使用jQuery移动的来提供特定于移动的事件:http://api.jquerymobile.com/category/events/

s5a0g9ez

s5a0g9ez2#

仅使用JavaScript

您可以使用“touchstart”事件进行单次触摸,
但是通过计算他应该再次点击的时间
我使用400(0.4s),因为它是两次触摸之间的较长持续时间
这只是一个估计,但它仍然是一个合理的时间

let expired

let doubleClick = function () {
    console.log('double click')
}

let doubleTouch = function (e) {
    if (e.touches.length === 1) {
        if (!expired) {
            expired = e.timeStamp + 400
        } else if (e.timeStamp <= expired) {
            // remove the default of this event ( Zoom )
            e.preventDefault()
            doubleClick()
            // then reset the variable for other "double Touches" event
            expired = null
        } else {
            // if the second touch was expired, make it as it's the first
            expired = e.timeStamp + 400
        }
    }
}

let element = document.getElementById('btn')
element.addEventListener('touchstart', doubleTouch)
element.addEventListener('dblclick', doubleClick)

字符串

如果出现此错误:

  • 由于目标被视为被动,无法在被动事件侦听器内防止默认值。*

如果element =“document”或“document.body”,则event.preventDefault()不起作用
因此,解决方案,你应该有一个完整的页面div容器:

let element = document.getElementById('container')
element.style.minWidth = '100vw'
element.style.minHeight = '100vh'
document.body.style.margin = '0px'
element.addEventListener('touchstart', elementTouch)
element.addEventListener('dblclick', doubleClick)

rur96b6h

rur96b6h3#

我自己把它固定成这样:

this.canvas.addEventListener('touchend', (e) => {
    // time of current press
    const currentTime = new Date().getTime()
    // time between presses
    const tapLength = currentTime - this.lastTap

    if (tapLength < 500 && tapLength > 0) {
        // if double click
        e.preventDefault()
        this.avatarInput.click()
    } else {
        // if single click
        this.isPinching = false
    }
    // remember the time of the last touch
    this.lastTap = currentTime
})

字符串

相关问题