How to prevent an element from losing focus?

后端 未结 6 1806
忘掉有多难
忘掉有多难 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:55

    Konstantin Dinev answer works ok in most cases, but if you dont want to lose the focus only when clicking on certain parts of the html just do this:

    $(".nofocus").on( "mousedown", function (e) {
        return false;
    });
    

    In my case i'm doing a small html text editor and i dont want to lose the control when pressing an action button, but yes in any other case.

    I just need to add the nofocus class to the button and it will not take the control

提交回复
热议问题