


js imitates html5 placeholder to adapt to unsupported browsers_javascript skills
HTML5 natively supports placeholder. For browsers that do not support it (ie), js simulation can be used.
js code
( function(){
//Determine whether placeholder is supported
function isPlaceholer(){
var input = document.createElement('input');
return "placeholder" in input;
}
//Unsupported code
if(!isPlaceholer()){
//Create a class
function Placeholder(obj){
this.input = obj;
this .label = document.createElement('label');
this.label.innerHTML = obj.getAttribute('placeholder');
this.label.style.cssText = 'position:absolute; text-indent: 4px;color:#999999; font-size:12px;';
if(obj.value != ''){
this.label.style.display = 'none';
}
this.init();
}
Placeholder.prototype = {
//Get the position
getxy: function(obj){
var left, top;
if(document .documentElement.getBoundingClientRect){
var html = document.documentElement,
body = document.body,
pos = obj.getBoundingClientRect(),
st = html.scrollTop || body.scrollTop,
sl = html.scrollLeft || body.scrollLeft,
ct = html.clientTop || body.clientTop,
cl = html.clientLeft || body.clientLeft;
left = pos.left sl - cl;
top = pos.top st - ct;
}
else{
while(obj){
left = obj.offsetLeft;
top = obj.offsetTop ;
obj = obj.offsetParent;
}
}
return{
left: left,
top : top
}
},
// Get width and height
getwh : function(obj){
return {
w : obj.offsetWidth,
h : obj.offsetHeight
}
},
//Add Width and height value method
setStyles: function(obj,styles){
for(var p in styles){
obj.style[p] = styles[p] 'px';
}
},
init : function(){
var label = this.label,
input = this.input,
xy = this.getxy(input),
wh = this .getwh(input);
this.setStyles(label, {'width':wh.w, 'height':wh.h, 'lineHeight':20, 'left':xy.left, 'top': xy.top});
document.body.appendChild(label);
label.onclick = function(){
this.style.display = "none";
input.focus() ;
}
input.onfocus = function(){
label.style.display = "none";
};
input.onblur = function(){
if( this.value == ""){
label.style.display = "block";
}
};
}
}
var inpColl = document.getElementsByTagName(' input'),
textColl = document.getElementsByTagName('textarea');
//HTML collection is converted into an array
function toArray(coll){
for(var i = 0, a = [ ], len = coll.length; i < len; i ){
a[i] = coll[i];
}
return a;
}
var inpArr = toArray (inpColl),
textArr = toArray(textColl),
placeholderArr = inpArr.concat(textArr);
for (var i = 0; i < placeholderArr.length; i ){
if (placeholderArr[i].getAttribute('placeholder')){
new Placeholder(placeholderArr[i]);
}
}
}
})()
html code:
< ;br>
css code:
div,input,textarea{ margin:0 ; padding:0;}
div{width:400px; margin:100px auto 0;}
input,textarea{width:200px;height:20px; margin-top:5px;line-height:20px;border :1px #666666 solid; background-color:#fff; padding-left:2px;}
textarea{ height:60px; font-size:12px; resize:none;}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
