jquery plug-in Implementation-Custom placeholder

Source: Internet
Author: User
Tags bind

Here a simple introduction to the realization of ideas.

1. Performance is as similar as the placeholder of HTML5 native
2. Progressive enhancements do not handle browsers that support placeholder
1. First, there are several tool methods:

Supportproperty (NodeType, property), to obtain whether the browser supports one of the properties of a control
Getpositionindoc (target, parent), gets the location of the object in the document
$c, a quick way to create a DOM object
These tools are some of the more common methods, if you have your own or more appropriate to replace.

2. Main body, Customplaceholder object. This object is primarily to maintain information about each text box, including its location, the hint information that should be displayed, and so on, and it also contains methods for creating hints and positioning, and corresponding events for objects.

Events are handled mainly in the Initevents function, where it is particularly important to note the handling of informational events, where the focus should be repositioned to the text box when the cue message is clicked. The text box is to handle focus and blur events.

The code is as follows Copy Code

$ (Self.hint). bind (' click ', Function (e) {
Self.input.focus ();
});

$ (self.input). Bind (' Focus ', function (e) {
Self.hint.style.display = ' None ';
});

$ (self.input). bind (' blur ', function (e) {
if (This.value = = "") {
Self.hint.style.display = ' inline ';
}
});

The two main methods for Customplacehodler objects are Createhintlabel (text, position) and position (). Createhintlabel is the DOM object that is used to create the hint information and locates it and returns the object. The position method is used to force a relocation of the prompt message. Mainly used in the case of page size change. Both of these methods are simpler to function and implement.

3. The function realization part of the plug-in. The jquery plug-in implementation is not much to say. In this case, the ability test is performed first, if the native support placeholder is returned directly.

The code is as follows Copy Code
if (supportproperty (' input ', ' placeholder ')) {
Return

The next step is to generate the corresponding Customplaceholder object, save in the array, and get the information about each object's cue, add to the container, and finally attach the container to the body object, based on the input object you select.

The code is as follows Copy Code

var customplaceholders = [];
if (This.length > 0) {
var box = $c (' div ', ' dk_placeholderfixed_box ');
for (var i = 0, len = this.length i < len; i++) {
var input = this[i];
Customplaceholders.push (New Customplaceholder (box, input, option));
}

Document.body.appendChild (box);
}

Finally, a more important thing is to bind the Resize event for the Window object and reposition all Customplacehoder objects when the Window object triggers the Resize event.

The code is as follows Copy Code

$ (window). Bind (' Resize ', function (e) {
for (var i = 0, len = customplaceholders.length i < len; i++) {
var customplaceholder = customplaceholders[i];
Customplaceholder.position ();
}

});

This simple widget will be finished here.

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.