相关文章推荐
眉毛粗的书签  ·  第四版·  1 年前    · 
朝气蓬勃的饭盒  ·  如果iframe ...·  1 年前    · 
腼腆的小熊猫  ·  PyAutoGUI ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am setting the focus on a certain input field when loading a page using the following line:

$('#myInputID').focus();

Is there a way that I can undo or remove this focus when hovering over a certain element? (The focus does not have to be reset after leaving this element.)

I couldn't find a function that is the opposite to the above in jQuery or would otherwise work here.

The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input>. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.

$("#myInputID").blur(); 
                Ok, so I think I am doing something wrong here or I described this the wrong way. The element I want to use here has the class navbar (it is a Bootstrap 3 navbar) so I tried the following but this does not work: $('.navbar').on('mouseover', function() { $('#myInputID').blur(); });
– user2571510
                Sep 24, 2014 at 7:34
                Yes, I did that. If this does not work could I just set the focus on the navbar instead ?
– user2571510
                Sep 24, 2014 at 7:50

$(':text').attr("disabled", "disabled"); sets all textbox to disabled mode. You can do in another way like giving each textbox id. By doing this code weight will be more and performance issue will be there.

So better have $(':text').attr("disabled", "disabled"); approach.

If you have readonly attribute, blur by itself would not work. Contraption below should do the job.

$('#myInputID').removeAttr('readonly').trigger('blur').attr('readonly','readonly');

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.