将placeholder-label.js作为外部js添加在下方。
( function( $ ) {
var IE10 = !!(navigator && navigator.userAgent && navigator.userAgent.match('MSIE 10'));
var IE11 = !!(navigator && navigator.userAgent && navigator.userAgent.match(/Trident.*rv\:11\./));
var overridePlaceholder = (IE10 || IE11);
$.fn.placeholder_labels = function(options) {
var that = this;
var defaults = {
blurColor : '#aaaaaa',
focusColor : '#bbbbbb',
labelClass : 'placeholder-label'
}
var settings = $.extend({}, defaults, options);
if ('placeholder' in document.createElement('input') && !overridePlaceholder) {
return this;
}
this.each(function(){
var $input = $( this );
var placeholder_text = $input.attr( 'placeholder' );
if (overridePlaceholder) $input.attr('placeholder', '');
var line_height = {};
if( !$input.is('textarea') && $input.css('height') != 'auto') {
line_height = { lineHeight: '23px', whiteSpace: 'nowrap' };
}
var $label = $( '<label></label>' ).addClass(settings.labelClass);
$label.css("width", "100%");
var $span = $( '<span>' + placeholder_text + '</span>' );
$span.appendTo( $label );
$span.css($.extend({
position:'absolute',
display: 'inline',
float: 'none',
overflow: 'hidden',
textAlign: 'left',
color: settings.blurColor,
cursor: 'text',
paddingTop: $input.css('padding-top'),
paddingRight: $input.css('padding-right'),
paddingBottom: $input.css('padding-bottom'),
paddingLeft: $input.css('padding-left'),
marginTop: $input.css('margin-top'),
marginRight: $input.css('margin-right'),
marginBottom: $input.css('margin-bottom'),
marginLeft: $input.css('margin-left'),
fontSize: $input.css('font-size'),
fontFamily: $input.css('font-family'),
fontStyle: $input.css('font-style'),
fontWeight: $input.css('font-weight'),
textTransform: $input.css('text-transform'),
backgroundColor: 'transparent',
zIndex: 99
}, line_height));
$('#typefi-btn-clear-filter').insertAfter('#typefi-files-search-input');
$label.insertBefore( $input ).append( $input );
var w = $input.width(),
h = $input.height();
if ( !w || !h ) {
var $clone = $input.clone().appendTo( 'body' ).css({
position : 'absolute',
visibility : 'hidden',
display : 'block'
});
w = $clone.width();
h = $clone.height();
$clone.remove();
}
$input.on('hide', function() {
$span.hide();
});
$input.on('show', function(){
$span.show().css({
width:$input.width()+'px',
height:$input.height()+'px'
});
});
if ( !$input.is(':visible') || $input.val().length ) {
$span.hide();
}
$input
.on("keydown keyup", function() {
if ( $(this).val() === "" ) {
$span.show();
} else {
$span.hide();
}
})
.focus( function() {
$span
.addClass( "input-selected" )
.css('color', settings.focusColor);
})
.blur( function() {
$span
.removeClass( "input-selected" )
.css('color', settings.blurColor);
});
$span.focus( function() {
$input.focus();
});
if($input.attr("autofocus")){
$input.focus();
}
});
var hide = $.fn.hide;
$.fn.hide = function() {
this.trigger('hide');
return hide.apply(this, arguments);
};
var show = $.fn.show;
$.fn.show = function() {
this.trigger('show');
return show.apply(this, arguments);
};
return that;
};
})( jQuery );
然后在 HTML 中如下链接 js。
<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js\"></script>
<script src="/js/placeholder-label.js"></script>
<script>
$( function() {
$("input[placeholder], textarea[placeholder]").placeholder_labels();
});
</input>
。您可能希望通过HTML验证器运行页面以查看是否有其他错误。 - j08691