2011-09-20 75 views
0

我在代碼中需要一些幫助。使用javascript的水印文本框

我想要的代碼動態就像在JavaScript中使用類,但我沒有太強大使用JavaScript。我需要你的解決方案。

例如,如果有人輸入文本框,文本框顯示文本「輸入關鍵詞或短語」。文字'輸入關鍵詞或短語'消失。如果不輸入或鼠標懸停在文本框上,則文本「輸入關鍵詞或短語」再次以灰色顯示。 我的代碼如下:

<asp:TextBox ID="txtQuery" runat="server" style="color: Gray;" 
ValidationGroup="req" value='Enter key word or phrase' 
onblur="if (this.value == '') { this.value = 'Enter key word or phrase'; 
this.style.color = 'Gray'; }" 
maxlength="255" onfocus="if(this.value == this.defaultValue){this.value=''; 
this.style.color='Black'}"></asp:TextBox> 

請幫忙。

+0

您的目標是搜索什麼東西?數據庫或使用想要使用Google搜索。 – adatapost

+0

我不認爲我理解你的問題,因爲你的代碼有效。看到這個jsfiddle:http://jsfiddle.net/NdkwJ/ – Icarus

+0

你試圖使用代碼添加它後面 – Binil

回答

0

因爲這樣的代碼應該可以工作,但是您還需要處理一些場景,例如在驗證java腳本中的文本框的同時將文本提交或視爲空白文本時清除文本框。

如果您使用的是jQuery,那麼上述功能幾乎沒有插件可用。例如,看看labelify

0
<asp:TextBox ID="txtQuery" CssClass="baseVal" runat="server" Text="Enter key word or phrase"></asp:TextBox> 
<script type='text/javascript'> 
     function ClearMLSText() { 
      if ($("#txtQuery").val() == "Enter key word or phrase") { 
       $("#txtQuery").val(""); 
       $("#txtQuery").addClass("hasVal").removeClass("baseVal"); 
      } 
     } 

     function BaseMLSText() { 
      if ($("#txtQuery").val() == "") { 
       $("#txtQuery").val("Enter key word or phrase"); 
       $("#txtQuery").addClass("baseVal").removeClass("hasVal"); 
      } 
     } 
</script> 

<style type="text/css"> 
#txtQuery.baseVal { color: #999; font-style: italic; } 
#txtQuery.hasVal { color: #000; font-style: normal; } 
</style> 

你可能也想看看jQuery的水印插件Get WaterMark Plugin

0

水印文本框使用javascript:

在Ajax工具包也可以用文本框被添加水印擴展每當文本框聚焦時,寫入其中的默認文本被清除。你可以用JavaScript做更多的JavaScript或jQuery,而不是使用ajax工具包,因爲它會降低服務器的速度,讓頁面呈現。

對於直播示例訪問:http://www.dotnetchallengers.com/App_Folder/Content/CodeSnippets/Java%20Script/WaterMark%20a%20Text%20box%20Using%20javascript%20Java%20Script/WaterMark_a_Text_box_Using_javascript_Java_Script.aspx?TutorialIds=17