placeholder

摘要:
HTML5本机支持占位符。对于不支持js的浏览器,可以使用js来模拟实现。

html5原生支持placeholder,对于不支持的浏览器(ie),可用js模拟实现。

(function(){
//判断是否支持placeholder
function isPlaceholer(){
var input = document.createElement('input');
return "placeholder" in input;
}
//不支持的代码
if(!isPlaceholer()){
//创建一个类
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 = {
//取位置
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
}
},
//取宽高
getwh : function(obj){
return {
w : obj.offsetWidth,
h : obj.offsetHeight
}
},
//添加宽高值方法
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集合转化为数组
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代码

<div>
<input type="text" placeholder="提示1" /><br>
<textarea placeholder="提示2" /></textarea><br>
<input type="text" placeholder="提示3" /><br>
</div>

css代码

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;}

使用:将JS,在页面底部引入,直接使用placeholder属性即可。

免责声明:文章转载自《placeholder》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C++调用linux命令并获取返回值linux上部署vue项目下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

TP5.1 爬虫

环境 PHP >= 7.1 !!!PHP >= 7.1 !!!PHP >= 7.1 !!! 安装 composer require jaeger/querylist 后端 //爬虫 public functioncrawler() { if(request()->post()){...

搭建前端监控系统(备选)用户行为统计和监控篇(如何快速定位线上问题)

背景:市面上的监控系统有很多,大多收费,对于小型前端项目来说,必然是痛点。另一点主要原因是,功能虽然通用,却未必能够满足我们自己的需求, 所以我们自给自足也许是个不错的办法。 这是搭建前端监控系统的第二章,主要是介绍如何统计js报错,跟着我一步步做,你也能搭建出一个属于自己的前端监控系统。 =================================...

javascript占位符

传统javascript: The text is inserted in the value attribute. On focus, it checks if the value is "search" and returns empty to clear the field. If the value is empty, it returns "se...

Django:使用模态框新增数据,成功后提示“提交成功”,并刷新表格bootstrap-table数据

废话不说先看图:    代码实现:   前台代码: {% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>项目...

flask第30篇——宏macro和import标签

宏是Jinja2特有的,像Django则没有这个。 先新建一个项目macroDemo: 然后在templates文件夹中新建index.html文件,并在代码中返回渲染后的文件: 然后回到index.html,现在假设我们要写一个登录的表单: 代码: <!DOCTYPE html><html lang="en"><he...

iOS开发——你真的会用SDWebImage?

http://www.cocoachina.com/ios/20160503/16064.html 本文授权转载,作者:hosea_zhou(简书) SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高。但是你真的会用吗?本文接下来将通过例子分析如何合理使用SDWebImage。 使用场景:自定义的UITableViewCell上有图片需要...