简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

IE不支持HTML5表单属性placeholder的解决办法

来源:易贤网   阅读:1012 次  日期:2015-04-17 13:32:37

温馨提示:易贤网小编为您整理了“IE不支持HTML5表单属性placeholder的解决办法”,方便广大网友查阅!

IE,HTML5,表单,placeholder,具体代码如下:

(function ($) {

$.fn.placeholder = function (options) {

var defaults = {

pColor: "#ccc",

pActive: "#999",

pFont: "14px",

activeBorder: "#080",

posL: 8,

zIndex: "99"

},

opts = $.extend(defaults, options);

//

return this.each(function () {

if ("placeholder" in document.createElement("input")) return;

$(this).parent().css("position", "relative");

var isIE = $.browser.msie,

version = $.browser.version;

//不支持placeholder的浏览器

var $this = $(this),

msg = $this.attr("placeholder"),

iH = $this.outerHeight(),

iW = $this.outerWidth(),

iX = $this.position().left,

iY = $this.position().top,

oInput = $("<label>", {

"class": "test",

"text": msg,

"css": {

"position": "absolute",

"left": iX + "px",

"top": iY + "px",

"width": iW - opts.posL + "px",

"padding-left": opts.posL + "px",

"height": iH + "px",

"line-height": iH + "px",

"color": opts.pColor,

"font-size": opts.pFont,

"z-index": opts.zIndex,

"cursor": "text"

}

}).insertBefore($this);

//初始状态就有内容

var value = $this.val();

if (value.length > 0) {

oInput.hide();

};

//

$this.on("focus", function () {

var value = $(this).val();

if (value.length > 0) {

oInput.hide();

}

oInput.css("color", opts.pActive);

//

if(isIE && version < 9){

var myEvent = "propertychange";

}else{

var myEvent = "input";

}

$(this).on(myEvent, function () {

var value = $(this).val();

if (value.length == 0) {

oInput.show();

} else {

oInput.hide();

}

});

}).on("blur", function () {

var value = $(this).val();

if (value.length == 0) {

oInput.css("color", opts.pColor).show();

}

});

//

oInput.on("click", function () {

$this.trigger("focus");

$(this).css("color", opts.pActive)

});

//

$this.filter(":focus").trigger("focus");

});

}

})(jQuery)

调用方法:

//解决IE下不支持placeholder

if($.browser.msie) {

$(":input[placeholder]").each(function(){

$(this).placeholder();

});

}

更多信息请查看IT技术专栏

更多信息请查看脚本栏目
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:jquery+html+css实现城市三级联动
下一篇:javascript实现浏览器收藏网页到收藏夹
易贤网手机网站地址:IE不支持HTML5表单属性placeholder的解决办法
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!