jquery implements the text box defaults to induce mouse actions:
This section describes how to use jquery to implement the default value of the text box to induce mouse action.
For example, when the text box gets the mouse focus, the default value is emptied, and when the text box is not entered, the mouse focus is left, and the default value is restored.
Code instance:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Cloud-dwelling community </title>
<script src= "Http://libs.baidu.com/jquery/1.9.0/jquery.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#email"). focus (function () {
var email_txt = $ (this). Val ();
if (email_txt = = This.defaultvalue) {
$ (this). Val ("");
}
})
$ ("#email"). blur (function () {
var email_txt = $ (this). Val ();
if (Email_txt = = "") {
$ (this). Val (This.defaultvalue);
}
})
})
</script>
<body>
<p><input type= "text" value= "Please enter email address" id= "email"/></p>
</body>
The above code to achieve our requirements, the following brief introduction to its implementation principle:
It's very simple to register the focus and Blur event handler for the text box, when the text box gets the focus, if the content of the text box is the same as the default value, the text box is emptied, and when the focus leaves the text box, if the text box is empty, it reverts to the default value.
or use the following code:
Copy Code code as follows:
$ ('. Default-value '). each (function () {
var default_value = This.value;
$ (this). focus (function () {
if (this.value = = default_value) {
This.value = ';
}
});
$ (this). blur (function () {
if (This.value = = "") {
This.value = Default_value;
}
});
});