Onfocus,onblur event prompts for different text effects in JavaScript

Source: Internet
Author: User

Example

The mouse in the form disappears, moves out the prompt font to display

The code is as follows Copy Code

When the focus is captured
function Onfocusfun (element, Elementvalue) {
if (Element.value = = Elementvalue) {
Element.value = "";
}
}
Leave the input box to trigger the event
function Onblurfun (element, Elementvalue) {
if (Element.value = = "" | | Element.value.replace (/\s/g, "") = = "") {//mouse is replaced with null regardless of whether the value in the text box is empty
Element.value = Elementvalue; Assign a value to a text box
}
}
Usage: <input type= "text" class= "value=" "id=" username "name=" username "onfocus=" onfocusfun (this, ' username/mobile number ') "onblur= "Onblurfun (this, ' username/mobile number ')"/>

Now this is a very important time for Web page optimization, we can use jquery to implement and jquery code is more concise.

The code is as follows Copy Code

<input id= "Xf_search" type= "text"/>
<script type= "Text/javascript" >
(function () {$ ("#xf_search"). focus (function () {(this.value== ' Please enter the name of the property ') This.value= ': false} '; $ ("#xf_search"). Blur (function () {(this.value== ')? this.value= ' Please enter the name of the property ': false}); }());
</script>

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.