【发布时间】:2011-09-03 12:29:30
【问题描述】:
我一直在使用 HTML 5 占位符,但刚刚意识到它在 HTML5 设备之外不起作用。正如您在下面的代码中看到的那样,占位符始终为小写,而值始终为大写。
#maphead input::-webkit-input-placeholder {
text-transform:lowercase;
}
#maphead input:-moz-placeholder {
text-transform:lowercase;
}
<input id="start" type="text" spellcheck="false" placeholder="enter your post code" style="text-transform:uppercase;" class="capital"/>
除了处理非 HTML 5 设备外,这一切都很好。为此,我使用了一些糟糕的 javascript。
function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase();
if (detect.indexOf("safari") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
if (inputs[i].getAttribute("type") == "text") {
var placeholder = inputs[i].getAttribute("placeholder");
if (placeholder.length > 0 || value == placeholder) {
inputs[i].value = placeholder;
inputs[i].onclick = function() {
if (this.value == this.getAttribute("placeholder")) {
this.value = "";
}
return false;
}
inputs[i].onblur = function() {
if (this.value.length < 1) {
this.value = this.getAttribute("placeholder");
$('.capital').each(function() {
var current = $(this).val();
var place = $(this).attr('placeholder');
if (current == place) {
$(this).css('text-transform','lowercase')
}
});
}
}
}
}
}
}
window.onload = function() {
activatePlaceholders();
}
首先,这个 Javascript 是腐烂的。必须有一个更简单的 JQuery 方式。现在,尽管上述方法确实有效(合理地),但它不会响应将占位符保持为小写而将值保持为大写,因为它使用占位符设置值。
我已经为你们准备好了一把好听的 Fiddle http://jsfiddle.net/Z9YLZ/1/
【问题讨论】:
标签: javascript jquery html placeholder