简体   繁体   English

jQuery touchstart 不适用于模糊

[英]jQuery touchstart not working for blur

The issue I have here is with touchstart .我这里的问题是touchstart Whats happening instead of behaving like click when you click into another focused input it adds the wanted class to footer.当您单击另一个焦点输入时,发生了什么而不是像单击一样,它会将所需的 class 添加到页脚。 Touchstart doesn't. Touchstart没有。 Instead it seem to act to quickly for the blur and the blur happens after.相反,它似乎对模糊反应迅速,然后模糊发生。

Anyone know how to stop this so touchstart behaves like click?任何人都知道如何阻止这种情况,因此touchstart行为就像点击一样?

 $('input:not(:radio)').on('click touchstart', function (e) { if(e.type==='click'){}else{ $('footer').addClass('touched'); } }).on('blur', function () { $('.touched').removeClass('touched'); });
 footer{background:blue;display:block;height:50px;} footer.touched{background:red}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input type="text"> <input type="text"> <footer>footer</footer>

So figured out a way to get it to work and also eliminate any painful flickering of the footer animating between input focus'. 因此,想出了一种方法来使其工作并消除页脚动画在输入焦点之间的任何痛苦闪烁”。

 $('input:not(:radio)').on('click touchstart', function (e) { if(e.type==='click'){}else{ $('#form-footer').hide(); setTimeout(function(){ $('#form-footer').addClass('touched').show(); }, 400); } }).on('blur', function () { $('.touched').removeClass('touched'); }); 

I was having similiar issues trying to get a Bootstrap 5 button to work the same between mouse clicks and using touch screens.我遇到了类似的问题,试图让 Bootstrap 5 按钮在鼠标点击和使用触摸屏之间发挥相同的作用。

My required cause and affect as follows:我所需的原因和影响如下:

I'm using the same button to change a varibale from true to false, then press the same button again it reverts the var from false back to true.我使用相同的按钮将变量从真更改为假,然后再次按下相同的按钮,它会将变量从假恢复为真。 So in essence I'm using a single button to latch on, then press it again and it latches off.所以本质上,我使用一个按钮来锁定,然后再次按下它,它就会关闭。

In the code sample below, I am changing the style of the button based upon what mode it has been set to.在下面的代码示例中,我根据按钮设置的模式更改按钮的样式。

HTML: HTML:

<button class="btn btn-outline-secondary ms-2" type="button" data-bs-toggle="tooltip" data-bs-placement="right" title="Draw Connector" id="drawConnectorModeButton" data-bs-animation="false">
    <i class="fa-solid fa-highlighter-line fa-lg"></i>
</button>

JavaScript: JavaScript:

// Remove the blur from the bootsrtap buttons once released.
$(".btn").mouseup(function () {
    $(this).blur();
})

var drawConnectorModeActive = false;

$('#drawConnectorModeButton').on('click touchend', function (e) {
    e.preventDefault();
    $('#drawConnectorModeButton').tooltip('hide');
    if (drawConnectorModeActive == true) { // Off
        $('#drawConnectorModeButton').removeClass('btn-secondary').addClass('btn-outline-secondary');
        drawConnectorModeActive = false;
    } else { // On
        $(this).blur();
        $('#drawConnectorModeButton').removeClass('btn-outline-secondary').addClass('btn-secondary');
        drawConnectorModeActive = true;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM