gpt4 book ai didi

HTML5 占位符在焦点上消失

转载 作者:技术小花猫 更新时间:2023-10-29 11:47:58 24 4
gpt4 key购买 nike

是否有免费的 jQuery 插件可以更改 placeholder 行为以匹配 HTML5 规范?

对焦前
chrome unfocused placeholder

对焦良好(Safari)
safari focused placeholder

注意力不集中(Chrome、Firefox)
chrome focused placeholder

您可以使用浏览器的功能with this simple fiddle .

HTML5 draft spec says :

User agents should present this hint to the user, after having stripped line breaks from it, when the element's value is the empty string and/or the control is not focused (e.g. by displaying it inside a blank unfocused control and hiding it otherwise).

“/or”是当前草案中的新内容,所以我想这就是 Chrome 和 Firefox 尚不支持它的原因。参见 WebKit bug #73629 , Chromium bug #103025 .

最佳答案

Stefano labels

Stefano J. Attardi写了一个很好的 jQuery 插件,就是这样做的。
它比 Robert 的更稳定,并且在聚焦场时也会褪色为浅灰色。


我修改了他的插件以读取 placeholder 属性,而不是手动创建 span
This fiddle有完整代码:

HTML

<input type="text" placeholder="Hello, world!">

JS

// 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);

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; }
}

关于HTML5 占位符在焦点上消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8574356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com