How to prevent an element from losing focus?

后端 未结 6 1782
忘掉有多难
忘掉有多难 2020-12-30 01:36

How to keep the Focus on one textbox ? even if you click anywhere in a browser.

$(\"#txtSearch\").focus();
6条回答
  •  伪装坚强ぢ
    2020-12-30 01:47

    It's also possible without jQuery and without re-focusing, if you just need a click on specific elements to not grab the focus

    just listen for the mousedown event and cancel it

    element.addEventListener("mousedown", event => {event.preventDefault(); event.stopPropagation()});
    

提交回复
热议问题