how to prevent blur() running when clicking a link in jQuery?

前端 未结 4 1417
死守一世寂寞
死守一世寂寞 2020-11-27 07:12

i have:


and

$(\'input\').blur(function(){
    alert(\'stay focused!\');
});

4条回答
  •  忘掉有多难
    2020-11-27 07:29

    You can get this behavior by calling preventDefault() in the mousedown event of the control being clicked (that would otherwise take focus). For example:

    btn.addEventListener('mousedown', function (event) {
      event.preventDefault()
    })
    
    btn.addEventListener('click', function(ev) {
        input.value += '@'
        input.setSelectionRange(ta.value.length, ta.value.length)
    })
    

    See live example here.

提交回复
热议问题