如果有人遇到同样的问题,请设置为答案:) 并供将来参考,因为您似乎最终可能会使用该解决方案:http: //jsfiddle.net/NXAWW/
这将像其他页面中的示例一样在文本框中保留部分水印!祝你有个好的一天,
关键是这个
.keyup(function(){
var $input = $(this);
if ($input.val().trim() != '') {
$placeholder.val($input.val().trim() + $input.attr('placeholder').replace('test',''));
//$placeholder.val('');
} else {
$placeholder.val($input.val().trim() + $input.attr('placeholder').replace('test',''));
}
});
完整的 Jquery 代码
// Create placeholder input to serve as background
var $test = $('#test');
var $placeholder = $test.clone().removeAttr('id').removeAttr('placeholder').addClass('placeholder').val($test.attr('placeholder'));
var $container = $('<span class="placeholder-container"></span>');
$container.insertAfter($test).append($test).append($placeholder);
// Basic styling
$container.css({
position: 'relative'
});
$test.css({
position: 'absolute',
left: 0,
top: 0,
zIndex: 100,
backgroundColor: 'transparent',
borderColor: 'transparent'
});
$placeholder.css('color', 'transparent');
// Behavior for focus and blur to achieve the visual effect
$test.focus(function(){
var $input = $(this);
var $placeholder = $('.placeholder', $input.parent());
$placeholder.css('color', '#e0e0e0');
}).blur(function(){
var $input = $(this);
var $placeholder = $('.placeholder', $input.parent());
if ($input.val() == '')
$placeholder.css('color', 'transparent');
}).keyup(function(){
var $input = $(this);
if ($input.val().trim() != '') {
$placeholder.val($input.val().trim() + $input.attr('placeholder').replace('test',''));
//$placeholder.val('');
} else {
$placeholder.val($input.val().trim() + $input.attr('placeholder').replace('test',''));
}
});