Dan*_*mov 27 safari html5 focus placeholder jquery-plugins
是否有一个免费提供的jQuery插件可以改变placeholder行为以匹配HTML5规范?
聚焦之前
关注焦点(Safari)

焦点不好(Chrome,Firefox)

你可以用这个简单的小提琴你的浏览器做什么.
用户代理在向其删除换行符之后,当元素的值为空字符串和/或控件未聚焦时(例如,通过将其显示在空白的未聚焦控件内并以其他方式隐藏),应向用户显示此提示.
"/或"在当前草案中是新的,所以我想这就是为什么Chrome和Firefox还不支持它.请参阅WebKit错误#73629,Chromium bug#103025.
Dan*_*mov 15

Stefano J. Attardi写了一个不错的jQuery插件就是这么做的.
它比罗伯特更稳定,并且当场地聚焦时也会变淡.
我修改了他的插件来读取placeholder属性,而不是手动创建一个span.
这个小提琴有完整的代码:
<input type="text" placeholder="Hello, world!">
Run Code Online (Sandbox Code Playgroud)
// Original code by Stefano J. Attardi, MIT license
(function($) {
function toggleLabel() {
var input = $(this);
if (!input.parent().hasClass('placeholder')) {
var label = $('<label>').addClass('placeholder');
input.wrap(label);
var span = $('<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 = $('<label></label>').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', function() {
$(this).prev('span').css('color', '#999');
});
$(function() {
$('input[placeholder], textarea[placeholder]').each(
function() { toggleLabel.call(this); }
);
});
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
.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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33623 次 |
| 最近记录: |