我一直在使用HTML 5佔位符,只是意識到它不能在HTML5設備之外工作。正如您在下面的代碼中看到的,佔位符總是小寫,並且值始終爲大寫。JQuery佔位符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是rancid。必須有一個更簡單的JQuery方式。現在,雖然上面的這些工作確實有效(合理地),但它不響應將佔位符保持爲小寫,並且該值以大寫形式,因爲它使用佔位符來設置值。
我設置你所有了一個漂亮的小提琴http://jsfiddle.net/Z9YLZ/1/
好吧,它仍然有小寫的問題,但很難否認代碼是整潔的。 – Spudley 2011-05-27 16:02:56
我完全同意... – 2011-05-27 16:05:49
.empty {text-transform:lowercase; }在CSS中應該處理大小寫轉換。編輯:...除非遇到級聯問題,那麼您可以明確聲明樣式。 – 2011-05-27 16:06:05