文本框input, type为text,或者password,有一个属性placeholder,在firefox浏览器下当输入的时候,占位符就会消失这个属性非常好用,然而在IE下,就没有这效果,现在提供两种jQuery插件解决方式。
1.引入js,在加载页面时候执行:$('input,text').placeholder(); js的具体代码如下:
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
// Opera Mini v7 doesn't support placeholder although its DOM seems to indicate so
var isOperaMini = Object.prototype.toString.call(window.operamini) === '[object OperaMini]';
var isInputSupported = 'placeholder' in document.createElement('input') && !isOperaMini;
var isTextareaSupported = 'placeholder' in document.createElement('textarea') && !isOperaMini;
var valHooks = $.valHooks;
var propHooks = $.propHooks;
var hooks;
var placeholder;
var settings = {};
if (isInputSupported && isTextareaSupported) {
placeholder = $.fn.placeholder = function() {
return this;
};
placeholder.input = true;
placeholder.textarea = true;
} else {
placeholder = $.fn.placeholder = function(options) {
var defaults = {customClass: 'placeholder'};
settings = $.extend({}, defaults, options);
return this.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
.not('.'+settings.customClass)
.bind({
'focus.placeholder': clearPlaceholder,
'blur.placeholder': setPlaceholder
})
.data('placeholder-enabled', true)
.trigger('blur.placeholder');
};
placeholder.input = isInputSupported;
placeholder.textarea = isTextareaSupported;
hooks = {
'get': function(element) {
var $element = $(element);
var $passwordInput = $element.data('placeholder-password');
if ($passwordInput) {
return $passwordInput[0].value;
}
return $element.data('placeholder-enabled') && $element.hasClass(settings.customClass) ? '' : element.value;
},
'set': function(element, value) {
var $element = $(element);
var $replacement;
var $passwordInput;
if (value !== '') {
$replacement = $element.data('placeholder-textinput');
$passwordInput = $element.data('placeholder-password');
if ($replacement) {
clearPlaceholder.call($replacement[0], true, value) || (element.value = value);
$replacement[0].value = value;
} else if ($passwordInput) {
clearPlaceholder.call(element, true, value) || ($passwordInput[0].value = value);
element.value = value;
}
}
if (!$element.data('placeholder-enabled')) {
element.value = value;
return $element;
}
if (value === '') {
element.value = value;
// Setting the placeholder causes problems if the element continues to have focus.
if (element != safeActiveElement()) {
// We can't use `triggerHandler` here because of dummy text/password inputs :(
setPlaceholder.call(element);
}
} else {
if ($element.hasClass(settings.customClass)) {
clearPlaceholder.call(element);
}
element.value = value;
}
// `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
return $element;
}
};
if (!isInputSupported) {
valHooks.input = hooks;
propHooks.value = hooks;
}
if (!isTextareaSupported) {
valHooks.textarea = hooks;
propHooks.value = hooks;
}
$(function() {
// Look for forms
$(document).delegate('form', 'submit.placeholder', function() {
// Clear the placeholder values so they don't get submitted
var $inputs = $('.'+settings.customClass, this).each(function() {
clearPlaceholder.call(this, true, '');
});
setTimeout(function() {
$inputs.each(setPlaceholder);
}, 10);
});
});
// Clear placeholder values upon page reload
$(window).bind('beforeunload.placeholder', function() {
$('.'+settings.customClass).each(function() {
this.value = '';
});
});
}
function args(elem) {
// Return an object of element attributes
var newAttrs = {};
var rinlinejQuery = /^jQuery\d+$/;
$.each(elem.attributes, function(i, attr) {
if (attr.specified && !rinlinejQuery.test(attr.name)) {
newAttrs[attr.name] = attr.value;
}
});
return newAttrs;
}
function clearPlaceholder(event, value) {
var input = this;
var $input = $(input);
if (input.value === $input.attr('placeholder') && $input.hasClass(settings.customClass)) {
input.value = '';
$input.removeClass(settings.customClass);
if ($input.data('placeholder-password')) {
$input = $input.hide().nextAll('input[type="password"]:first').show().attr('id', $input.removeAttr('id').data('placeholder-id'));
// If `clearPlaceholder` was called from `$.valHooks.input.set`
if (event === true) {
$input[0].value = value;
return value;
}
$input.focus();
} else {
input == safeActiveElement() && input.select();
}
}
}
function setPlaceholder(event) {
var $replacement;
var input = this;
var $input = $(input);
var id = input.id;
// If the placeholder is activated, triggering blur event (`$input.trigger('blur')`) should do nothing.
if (event && event.type === 'blur') {
if ($input.hasClass(settings.customClass)) {
return;
}
if (input.type === 'password') {
$replacement = $input.prevAll('input[type="text"]:first');
if ($replacement.length > 0 && $replacement.is(':visible')) {
return;
}
}
}
if (input.value === '') {
if (input.type === 'password') {
if (!$input.data('placeholder-textinput')) {
try {
$replacement = $input.clone().prop({ 'type': 'text' });
} catch(e) {
$replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
}
$replacement
.removeAttr('name')
.data({
'placeholder-enabled': true,
'placeholder-password': $input,
'placeholder-id': id
})
.bind('focus.placeholder', clearPlaceholder);
$input
.data({
'placeholder-textinput': $replacement,
'placeholder-id': id
})
.before($replacement);
}
input.value = '';
$input = $input.removeAttr('id').hide().prevAll('input[type="text"]:first').attr('id', $input.data('placeholder-id')).show();
} else {
var $passwordInput = $input.data('placeholder-password');
if ($passwordInput) {
$passwordInput[0].value = '';
$input.attr('id', $input.data('placeholder-id')).show().nextAll('input[type="password"]:last').hide().removeAttr('id');
}
}
$input.addClass(settings.customClass);
$input[0].value = $input.attr('placeholder');
} else {
$input.removeClass(settings.customClass);
}
}
function safeActiveElement() {
// Avoid IE9 `document.activeElement` of death
try {
return document.activeElement;
} catch (exception) {}
}
}));
2.第二js插件可以通过传参设置属性,属性包括input的id,默认要显示的类型,以及点击前后显示的颜色,在页面引用js只需调用方法就行:如 :
placholdInput('请输入....','input-black','input-gray');具体js代码如下:
/*
* input placholder功能
* @id input的id
* @tip 默认显示的信息
* @classValue1 正常字体样式
* @classValue2 灰色字体样式
*/
function plachold(id, tip, classValue1, classValue2) {
$('#'+id).focus(function(){
var idValue=$('#'+id).val();
if(idValue==tip){
$(this).val('');
$(this).attr("class",classValue1);
}
})
$('#'+id).blur(function(){
var txt1_value=$('#'+id).val();
if(txt1_value==""){
$(this).val(tip);
$(this).attr("class", classValue2);
}
})
}
function placholdInput(tip, classValue1, classValue2){
$('input[type="text"]').each(function(){
var _this = $(this);
var defaultVal=_this.val();
if(defaultVal==tip){
_this.addClass(classValue2);
}
_this.focus(function(){
var idValue=_this.val();
if(idValue==tip){
_this.val('');
_this.removeClass(classValue2);
_this.addClass(classValue1);
}
})
_this.blur(function(){
var txt1_value=_this.val();
if(txt1_value==""){
_this.val(tip);
_this.removeClass(classValue1);
_this.addClass(classValue2);
}
})
});
}