我們需要四個東西:圖片、樣式表、jquery庫、頁面代碼
全部代碼在這裡:http://pan.baidu.com/share/link?shareid=200436&uk=2266385818
圖片見demo裡面:下面我們一個一個來:
1.css
/*=================全域樣式【start】======================*/*{scrollbar-face-color:#E6F2FD;scrollbar-highlight-color:#ffffff;scrollbar-shadow-color:#D6E4EF; scrollbar-3dlight-color:#F1F9FF; scrollbar-arrow-color:#006699; scrollbar-track-color:#F1F9FF; scrollbar-darkshadow-color:#F1F9FF;}body{margin:0px;color:#666;background-color:#FFF;font-family:Verdana, Geneva, sans-serif;font-size:12px;}form,ul,li{margin:0px;list-style-type:none;padding:0px;}table { empty-cells: show; border-collapse: collapse; }a{color:#005eac;text-decoration:none;}a:visited,a:active{ color:#005eac; }a:hover{ color:#ff5500; }img{ border:0px; vertical-align:middle; }/*=================全域樣式【end】======================*/.input{padding:0px 3px;border:1px solid #d1d1d1;background:url(input_bg.jpg) repeat-x;height:18px;line-height:18px;font-size:12px;color:#999;}.textarea{padding:3px;border:1px solid #d1d1d1;background:url(input_bg.jpg) repeat-x;font-size:12px;color:#999;}.focus{border:1px solid #FC0 !important;background:url(focus_bg.jpg) repeat-x !important;color:#333 !important;}/*提示文字樣式*/#HintMsg{width:271px;position:absolute;display:none;}#HintMsg .HintTop{height:9px;background:url(hintbg1.gif) no-repeat;overflow:hidden;}#HintMsg .HintInfo{padding:0 5px;border-left:1px solid #000;border-right:1px solid #000;background:#FFFFE1;line-height:1.5em;}#HintMsg .HintInfo b{display:block;margin-bottom:6px;padding-left:15px;background:url(hint.gif) left center no-repeat;height:13px;line-height:16px;}#HintMsg .HintInfo b span{display:block;float:right;text-indent:-9999px;background:url(close.gif) no-repeat;width:12px;height:12px;cursor:pointer;}#HintMsg .HintFooter{height:22px;background:url(hintbg2.gif) no-repeat;}/*設定提示樣式*/
2.頁面代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>文字框提示demo</title> <link href="images/example.css" rel="stylesheet" type="text/css" /> </head><body> <form id="form1" runat="server"> <div style=" margin:0 auto; width:600px;"> <div style=" height:100px;"></div> <asp:TextBox ID="txtA" runat="server" CssClass="input" HintTitle="提示" HintInfo="這是提示的內容你懂的" /> <br /><br /> <asp:TextBox ID="txtB" runat="server" CssClass="textarea" Height="130px" Width="500px" maxlength="250" HintTitle="標題你懂的" HintInfo="填寫一些內容" TextMode="MultiLine" /> </div> </form></body></html>
3.添加js到head裡面
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(".input,.textarea").focus(function () { $(this).addClass("focus"); }).blur(function () { $(this).removeClass("focus"); }); $("[HintTitle],[HintInfo]").focus(function (event) { $("*").stop(); //停止所有正在啟動並執行動畫 $("#HintMsg").remove(); var HintHtml = "<ul id=\"HintMsg\"><li class=\"HintTop\"></li><li class=\"HintInfo\"><b>" + $(this).attr("HintTitle") + "</b>" + $(this).attr("HintInfo") + "</li><li class=\"HintFooter\"></li></ul>"; var offset = $(this).offset(); //取得事件對象的位置 $("body").append(HintHtml); //添加節點 $("#HintMsg").fadeTo(0, 0.85); var HintHeight = $("#HintMsg").height(); //取得容器高度 $("#HintMsg").css({ "top": offset.top - HintHeight + "px", "left": offset.left + "px" }).fadeIn(500); }).blur(function (event) { $("#HintMsg").remove(); }); }); </script>
效果如下:
總結一下,這裡主要就是一個jq擷取對象的位置,然後添加一個節點