html5控制符置于底层,HTML5占位符在焦点上消失

Stefano J. Attardi写了一个不错的jQuery插件,只是这样。

它比罗伯特的更稳定,并且当场得到焦点时,还会变淡到更浅的灰色。

我修改他的插件读取占位符属性,而不是手动创建跨度。

This fiddle具有完整代码:

HTML

JS

// Original code by Stefano J. Attardi,MIT license

(function($) {

function toggleLabel() {

var input = $(this);

if (!input.parent().hasClass('placeholder')) {

var label = $('').addClass('placeholder');

input.wrap(label);

var span = $('');

span.text(input.attr('placeholder'))

input.removeAttr('placeholder');

span.insertBefore(input);

}

setTimeout(function() {

var def = input.attr('title');

if (!input.val() || (input.val() == def)) {

input.prev('span').css('visibility','');

if (def) {

var dummy = $('').text(def).css('visibility','hidden').appendTo('body');

input.prev('span').css('margin-left',dummy.width() + 3 + 'px');

dummy.remove();

}

} else {

input.prev('span').css('visibility','hidden');

}

},0);

};

function resetField() {

var def = $(this).attr('title');

if (!$(this).val() || ($(this).val() == def)) {

$(this).val(def);

$(this).prev('span').css('visibility','');

}

};

var fields = $('input,textarea');

fields.live('mouseup',toggleLabel); // needed for IE reset icon [X]

fields.live('keydown',toggleLabel);

fields.live('paste',toggleLabel);

fields.live('focusin',function() {

$(this).prev('span').css('color','#ccc');

});

fields.live('focusout','#999');

});

$(function() {

$('input[placeholder],textarea[placeholder]').each(

function() { toggleLabel.call(this); }

);

});

})(jQuery);

CSS

.placeholder {

background: white;

float: left;

clear: both;

}

.placeholder span {

position: absolute;

padding: 5px;

margin-left: 3px;

color: #999;

}

.placeholder input,.placeholder textarea {

position: relative;

margin: 0;

border-width: 1px;

padding: 6px;

background: transparent;

font: inherit;

}

/* Hack to remove Safari's extra padding. Remove if you don't care about pixel-perfection. */

@media screen and (-webkit-min-device-pixel-ratio:0) {

.placeholder input,.placeholder textarea { padding: 4px; }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值