記得引用jquery 類庫
復制代碼代碼如下:
$(document).ready(function () {
if ($.browser.msIE)
$("input:text,input:passWord").each(function () {
var $placeholder = $(this).attr("placeholder");
var $width = $(this).CSS("width");
var $id = $(this).attr("id");
var $height = parseInt($(this).CSS("height")) + 6 + "px";
var $fontSize = $(this).CSS("font-size");
var $fontWeight = $(this).CSS("font-weight");
var $lineHeight = $height;
if ($(this).CSS("line-height") != "normal") {
$lineHeight = parseInt($(this).CSS("line-height")) + 6 + "px";
}
if ($placeholder != undefined) {
$(this).after("<span class=/"placeholder ph_" + $id + "/" style=/"width:" + $width + ";line-height:" + $lineHeight + ";height:" + $height + ";font-weight:" + $fontWeight + ";margin-left:-" + $width + ";font-size:" + $fontSize + "/">" + $placeholder + "</span>");
}
$(this).bind("keyup", function () {
if ($(this).val() == "") {
$(this).parent().find(".ph_" + $id).CSS("display", "inline-block");
}
else {
$(this).parent().find(".ph_" + $id).CSS("display", "none");
}
});
});
$(".placeholder").live("click", function () {
$(this).prev().focus();
});
});
頁面調用
復制代碼代碼如下:
<input id="n1" type="text" placeholder="我是提示內容正常" />
<input id="n2" type="text" placeholder="我是提示內容寬高" style="width:100px;height:100px;" />
<input id="n3" type="text" placeholder="我是提示內容我有其他樣式" / style="width:300px;height:40px;font-weight:bold;">
<input id="n4" type="text" placeholder="還可以嘗試下其他的" />
樣式
復制代碼代碼如下:
<style type="text/CSS">
.placeholder {display:inline-block;color:gray;vertical-align:top;overflow:hidden;}
</style>