数码资讯
用jQuery实现的自定义占位符属性插件
HTML5中文本框的新属性是占位符,这是一个非常有用的属性。然而,IE系列不支持此属性到IE9,它让你毫不犹豫地使用这个属性的时候,我已经写了很多的小控件,类似于常见的,但它们不是很常见的,在这里分享一个jQuery插件逐渐增强的自定义占位符。它的使用比较简单,你也可以提高根据您的需要。通常写jQu
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
HTML5中文本框的新属性是占位符,这是一个非常有用的属性。然而,IE系列不支持此属性到IE9,它让你毫不犹豫地使用这个属性的时候,我已经写了很多的小控件,类似于常见的,但它们不是很常见的,在这里分享一个jQuery插件逐渐增强的自定义占位符。它的使用比较简单,你也可以提高根据您的需要。通常写jQuery插件少,考虑更多的jQuery的学生,这里与jQuery插件的形式。
这里简单介绍了这个想法的实现。
1。性能类似于HTML5的占位符。
2。增量增强不处理支持占位符的浏览器。
首先,它是几种工具方法:
1.supportproperty(节点类型,性能)得到一个浏览器支持一个控制一定的财产
2.getpositionindoc(目标、家长)到文档中的对象的位置
3、C,一种快速创建DOM对象的方法
这些工具都是常见的和常见的方法,如果您有自己的或更合适的,您可以自己替换它们。
二、主体的customplaceholder对象。这个对象主要是保持每个文本框的信息,包括位置、提示信息等。此外,它还包括创建提示和位置的方法以及对象的相应事件。
活动主要是在initevents函数处理。我们要注意处理突发事件。当提示信息时,重点应重新定位到文本框,文本框将处理的焦点和模糊事件。
复制代码代码如下所示:
$(自我暗示)。Bind(听到咔哒声,功能(e){ {)
Self.input.focus();
});
$(自我。输入)。Bind('focus',功能(e){ {)
self.hint.style.display =不关;
});
$(自己输入),Bind('blur功能(e){ {)
如果(this.value = =){
self.hint.style.display = '内联';
}
});
该customplacehodler对象的两种主要方法是createhintlabel(文本、位置)和位置()。CreateHintLabel是一个DOM对象,用于创建一个提示信息进行查找并返回对象位置的方法是用来把提示信息定位。它主要用于页面大小的变化。并对这两种方法实现的功能比较简单。
三。这样的plug-in.the jQuery插件实现的功能实现部分并不多。能力检测是第一次做,如果原生支持占位符直接返回。
复制代码代码如下所示:
如果(supportproperty(输入,'placeholder ')){
返回;
}
其次是产生相应的customplaceholder对象的基础上选定的输入对象,保存在数组中,并得到各对象的提示信息的DOM对象,添加到容器中,最后将容器体对象。
复制代码代码如下所示:
无功customplaceholders = { };
如果(this.length > 0){
VaR箱= $ C('div ','dk_placeholderfixed_box);
对于(var i = 0,len = this.length;我< len;i++){
var输入=此{ };
CustomPlaceholders.push(新customplaceholder(框,输入选项));
}
document.body.appendchild(箱);
}
最后,还有一件重要的事情是把调整窗口对象的事件,并将所有customplacehoder对象时触发事件的对象的大小的窗口。
复制代码代码如下所示:
$(窗口),Bind('resize功能(e){ {)
对于(var i = 0,len = customplaceholders.length;我< len;i++){
无功customplaceholder = customplaceholders {我};
customplaceholder.position();
}
});
这个简单的小插件在这里完成。
插入源代码:
(函数($){)
VaR模型= { {
DIV:document.createelement('div),
UL:document.createelement('ul),
李:document.createelement(李的),
跨度:document.createelement('span),
P:document.createelement('p'),
答:document.createelement(A),
片段:document.createdocumentfragment(),
输入:document.createelement(输入的)
}
无功supportproperty =功能(节点类型、属性){
开关(参数,长度){
案例0:
返回false;
案例1:
nodeType属性=,= 'div的节点类型;
资产= property.split(' ');
如果(property.length = = 1){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } };
} else if(property.length = = 2){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } } {财产{ 1 } };
}
案例2:
资产= property.split(' ');
如果(property.length = = 1){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } };
} else if(property.length = = 2){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } } {财产{ 1 } };
}
返回false;
违约:
返回false;
}
};
无功getpositionindoc =函数(目标、家长){
如果(!目标){
返回null;
}
var = 0,
顶部= 0;
{做
左= target.offsetleft | | 0;
最高target.offsettop | | 0;
目标= target.offsetparent;
如果(父目标=父){
打破;
}
而(目标)};
返回{
左:左,
顶:顶
};
}
变量$c =功能(tagname,ID,className){
var = null;
如果(!ELES { tagname }){
元= { } = tagname,document.createelement(TagName);
其他{ }
元= ELES { tagname }。clonenode(真的);
}
如果(id){
ele.id = ID;
}
如果(className){
ele.classname =类名;
}
返回元素;
};
无功customplaceholder =功能(框,输入选项){
var =;
VaR的位置= getpositionindoc(输入);
self.input =输入;
self.option = { xoffset:0,yoffset:0 };
对于(选项中的var项){
自选项{项} =选项{项};
}
self.hint = self.createhintlabel(input.getattribute('placeholder '),位置);
box.appendchild(自我暗示);
self.initevents =函数(){
$(自我暗示)。Bind(听到咔哒声,功能(e){ {)
Self.input.focus();
});
$(自我。输入)。Bind('focus',功能(e){ {)
self.hint.style.display =不关;
});
$(自己输入),Bind('blur功能(e){ {)
如果(this.value = =){
self.hint.style.display = '内联';
}
});
};
Self.initEvents();
};
customplaceholder.prototype = { {
CreateHintLabel:功能(文本、位置){
风险提示= $ C('label);
hint.style.cusor =中;
hint.style.position =绝对的;
hint.style.left = position.left + this.option.xoffset + 'px;
hint.style.top = position.top + this.option.yoffset + 'px;
hint.innerhtml =文本;
hint.style.zindex = '9999;
返回提示;
},
位置:函数(){
VaR的位置= getpositionindoc(这个输入);
this.hint.style.left = position.left + this.option.xoffset + 'px;
this.hint.style.top = position.top + this.option.yoffset + 'px;
}
};
美元。fn.placeholder =功能(可选){
如果(supportproperty(输入,'placeholder ')){
返回;
}
无功customplaceholders = { };
如果(this.length > 0){
VaR箱= $ C('div ','dk_placeholderfixed_box);
对于(var i = 0,len = this.length;我< len;i++){
var输入=此{ };
如果($(输入)为((可见))){
CustomPlaceholders.push(新customplaceholder(框,输入选项));
}
}
document.body.appendchild(箱);
}
$(窗口),Bind('resize功能(e){ {)
对于(var i = 0,len = customplaceholders.length;我< len;i++){
无功customplaceholder = customplaceholders {我};
customplaceholder.position();
}
});
};
}(jQuery);
这里简单介绍了这个想法的实现。
1。性能类似于HTML5的占位符。
2。增量增强不处理支持占位符的浏览器。
首先,它是几种工具方法:
1.supportproperty(节点类型,性能)得到一个浏览器支持一个控制一定的财产
2.getpositionindoc(目标、家长)到文档中的对象的位置
3、C,一种快速创建DOM对象的方法
这些工具都是常见的和常见的方法,如果您有自己的或更合适的,您可以自己替换它们。
二、主体的customplaceholder对象。这个对象主要是保持每个文本框的信息,包括位置、提示信息等。此外,它还包括创建提示和位置的方法以及对象的相应事件。
活动主要是在initevents函数处理。我们要注意处理突发事件。当提示信息时,重点应重新定位到文本框,文本框将处理的焦点和模糊事件。
复制代码代码如下所示:
$(自我暗示)。Bind(听到咔哒声,功能(e){ {)
Self.input.focus();
});
$(自我。输入)。Bind('focus',功能(e){ {)
self.hint.style.display =不关;
});
$(自己输入),Bind('blur功能(e){ {)
如果(this.value = =){
self.hint.style.display = '内联';
}
});
该customplacehodler对象的两种主要方法是createhintlabel(文本、位置)和位置()。CreateHintLabel是一个DOM对象,用于创建一个提示信息进行查找并返回对象位置的方法是用来把提示信息定位。它主要用于页面大小的变化。并对这两种方法实现的功能比较简单。
三。这样的plug-in.the jQuery插件实现的功能实现部分并不多。能力检测是第一次做,如果原生支持占位符直接返回。
复制代码代码如下所示:
如果(supportproperty(输入,'placeholder ')){
返回;
}
其次是产生相应的customplaceholder对象的基础上选定的输入对象,保存在数组中,并得到各对象的提示信息的DOM对象,添加到容器中,最后将容器体对象。
复制代码代码如下所示:
无功customplaceholders = { };
如果(this.length > 0){
VaR箱= $ C('div ','dk_placeholderfixed_box);
对于(var i = 0,len = this.length;我< len;i++){
var输入=此{ };
CustomPlaceholders.push(新customplaceholder(框,输入选项));
}
document.body.appendchild(箱);
}
最后,还有一件重要的事情是把调整窗口对象的事件,并将所有customplacehoder对象时触发事件的对象的大小的窗口。
复制代码代码如下所示:
$(窗口),Bind('resize功能(e){ {)
对于(var i = 0,len = customplaceholders.length;我< len;i++){
无功customplaceholder = customplaceholders {我};
customplaceholder.position();
}
});
这个简单的小插件在这里完成。
插入源代码:
(函数($){)
VaR模型= { {
DIV:document.createelement('div),
UL:document.createelement('ul),
李:document.createelement(李的),
跨度:document.createelement('span),
P:document.createelement('p'),
答:document.createelement(A),
片段:document.createdocumentfragment(),
输入:document.createelement(输入的)
}
无功supportproperty =功能(节点类型、属性){
开关(参数,长度){
案例0:
返回false;
案例1:
nodeType属性=,= 'div的节点类型;
资产= property.split(' ');
如果(property.length = = 1){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } };
} else if(property.length = = 2){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } } {财产{ 1 } };
}
案例2:
资产= property.split(' ');
如果(property.length = = 1){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } };
} else if(property.length = = 2){
返回类型= { } { 'undefined'eles nodeType属性{ 0 } } {财产{ 1 } };
}
返回false;
违约:
返回false;
}
};
无功getpositionindoc =函数(目标、家长){
如果(!目标){
返回null;
}
var = 0,
顶部= 0;
{做
左= target.offsetleft | | 0;
最高target.offsettop | | 0;
目标= target.offsetparent;
如果(父目标=父){
打破;
}
而(目标)};
返回{
左:左,
顶:顶
};
}
变量$c =功能(tagname,ID,className){
var = null;
如果(!ELES { tagname }){
元= { } = tagname,document.createelement(TagName);
其他{ }
元= ELES { tagname }。clonenode(真的);
}
如果(id){
ele.id = ID;
}
如果(className){
ele.classname =类名;
}
返回元素;
};
无功customplaceholder =功能(框,输入选项){
var =;
VaR的位置= getpositionindoc(输入);
self.input =输入;
self.option = { xoffset:0,yoffset:0 };
对于(选项中的var项){
自选项{项} =选项{项};
}
self.hint = self.createhintlabel(input.getattribute('placeholder '),位置);
box.appendchild(自我暗示);
self.initevents =函数(){
$(自我暗示)。Bind(听到咔哒声,功能(e){ {)
Self.input.focus();
});
$(自我。输入)。Bind('focus',功能(e){ {)
self.hint.style.display =不关;
});
$(自己输入),Bind('blur功能(e){ {)
如果(this.value = =){
self.hint.style.display = '内联';
}
});
};
Self.initEvents();
};
customplaceholder.prototype = { {
CreateHintLabel:功能(文本、位置){
风险提示= $ C('label);
hint.style.cusor =中;
hint.style.position =绝对的;
hint.style.left = position.left + this.option.xoffset + 'px;
hint.style.top = position.top + this.option.yoffset + 'px;
hint.innerhtml =文本;
hint.style.zindex = '9999;
返回提示;
},
位置:函数(){
VaR的位置= getpositionindoc(这个输入);
this.hint.style.left = position.left + this.option.xoffset + 'px;
this.hint.style.top = position.top + this.option.yoffset + 'px;
}
};
美元。fn.placeholder =功能(可选){
如果(supportproperty(输入,'placeholder ')){
返回;
}
无功customplaceholders = { };
如果(this.length > 0){
VaR箱= $ C('div ','dk_placeholderfixed_box);
对于(var i = 0,len = this.length;我< len;i++){
var输入=此{ };
如果($(输入)为((可见))){
CustomPlaceholders.push(新customplaceholder(框,输入选项));
}
}
document.body.appendchild(箱);
}
$(窗口),Bind('resize功能(e){ {)
对于(var i = 0,len = customplaceholders.length;我< len;i++){
无功customplaceholder = customplaceholders {我};
customplaceholder.position();
}
});
};
}(jQuery);
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。