합니다. 나는 이것이 좋다고 생각하지만 더 좋은 "XHTML"솔루션이있다 :
나는 태그를 기본으로 사용하고 자바 스크립트를 사용하면 숨기고 입력란에 복사하는 것이 더 좋을 것이라고 생각한다.
내 코드 :
(function($){
/**
* This function moves the input <label> into the input field
* Creates a new input box with jquery clone, and changes the type to text, so the password fields label will be also visible. on focus and blr only the new input field will hided or shown, depended on the value of the original field.
* Example of usage:
* @code
* $("input#foo").inputLabel();
* @endcode
*
* @require jquery.js
* @param none
* @return none
*/
$.fn.inputLabel = function() {
var input = this
var id;
var label;
var value;
var isDefault;
var fake;
id = input.attr("id"); //get the id of the input
label = $("label[for="+id+"]").hide(); //search for the label and hide it
value = label.html(); //get the label text
fake = input.clone();
fake.attr("type","text");
fake.attr("value",value);
input.after(fake);
input.hide();
isDefault = true;
var checkDefault = function() {
if(input.attr("value")=='') {
isDefault = true;
}
else {
isDefault = false;
}
}
fake.focusin(function() {
if(isDefault) {
fake.hide();
input.show();
input.focus();
}
});
input.focusout(function() {
if(isDefault) {
input.hide();
fake.show();
}
});
input.keydown(function(event) {
window.setTimeout(checkDefault,0);
});
$(window).unload(function(){ //firefox bug fix.
fake.attr("value","");
});
return this;
}
})(jQuery);
이 플러그인은 나에 의해 작성되었습니다. 쉽게 초점 등을 추가 할 수 있습니다이 후
$("#register-username,register-password").each($(this).inputLabel(););
을 : 수 있도록
<label for="register-username">username</label> <input id="register-username" type="text" class="register-ok" />
<label for="register-password">password</label> <input id="register-password" type="password" class="register" />
와 JS 코드를 : 당신이 사용하려는 경우
, 난 당신이 그런 식으로 이것을 사용한다고 생각합니다 스타일.
* 일부 * 작동하지 않습니까? – mattsven
IE에서 작동하지 않는 버전을 알려주는 것이 좋습니다 – polyhedron
@NeXXus, 포커스 효과를 의미합니다. – Paul