JS (jquery) resolves the input element's Blur event and other non-form element's Click event conflict methods

Source: Internet
Author: User

HTML structure: Very simple, just a input, a div, can explain the problem is OK;

<input type= "text" value= "default" ><br/><br/>
<div> Search </div>

1, the input box gets the focus when value is "", the value is "default" when the focus is lost;-----This is very good implementation;
2, when you enter the content in the input box, click on the div search, the control desk print output to search for the content (of course, the requirements of each project is different, here is just an example), and requires that the click does not affect the input focus and blur behavior;----This is the point.

Let's look at the effect before the conflict is resolved;
 $ ("input"). focus (function () {
This.value = "";
}). Blur (function () {
This.value = "Default value";
});

$ ("div"). Click (function () {
var value = $ ("input"). Val ();
Console.log (value);
});

result: enter "AAAA" in input, then click Div, the console output is "default", Inconsistent with the expected results;



resolve Method One: Add a timer to the Blur callback function, delaying the execution time of the Blur callback function, so that although the blur behavior of input is triggered when the div is clicked, the timer delay is added,
Therefore, the
is not executed until the click callback of the div finishes execution. The callback for the blur behavior of input;
$ ("input"). focus (function () {
This.value = "";
}). blur (function () {
var self=this;
SetTimeout (function () {
Self.value = "Default value";
},300)
});
$ ("div"). Click (function () {//This part is not changed
var value = $ ("input"). Val ();
Console.log (value);
});
Result: Input "AAAA" in input, and then click Div, the console output is "AAAA", in line with the expected results;



workaround Two: Change the click event of the Div to the MouseDown event, because The   mousedown behavior is triggered when the mouse punctuation goes down, and the click behavior is triggered when the mouse punctuation is lifted.
$ ("input"). focus (function () {//This part does not change
This.value = "";
}). blur (function () {
This.value = "Default value";
});
$ ("div"). MouseDown (function () {
var value = $ ("input"). Val ();
Console.log (value);
});
Result: Input "AAAA" in input, and then click Div, the console output is "AAAA", in line with the expected results;








JS (jquery) resolves the input element's Blur event and other non-form element's Click event conflict methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.