2013-04-08 103 views
0

我正嘗試創建一個文本區域,其中包含一些示例文本,當用戶開始輸入時該文本會消失。我有下面哪種作品的代碼。但...文本區域中的示例文本

默認的文本只出現一次我專注和textarea unfocus,我試着添加一個onload選項來顯示這個文本,但它不想爲我工作。

當我保存頁面而未觸及示例文本時,文本將變爲保存的文本顏色,而不是保持默認的灰色。

我不認爲我太遙遠,但這裏已經度過了大半天的兜兜,所有的幫助表示讚賞

 <textarea STYLE='background:white; height:80; width:100%;' name="notes" 
    onfocus="if (this.value == 'Example of what you need to do here') { 
     this.value = ''; 
     this.style.color = '#D10034'; 
    }" 
    onblur="if (this.value == '') { 
     this.value = 'Example of what you need to do here' 
     this.style.color = '#919191'; 
    }" 
    style="color:#D10034"; >$notes</textarea> 

回答

2

placeholder attribute提供了提示和示例文本。

<textarea name="nodes" placeholder="Example of what you need to do here"></textarea> 

沒有必要涉及JavaScript。

+0

我想到這一點,但我將使用將是相當大的,多行的例子中,在文本區域示例文本是較好的選擇 – 2013-04-08 09:25:19

+0

如果例子是複雜,如果用戶一開始輸入文字就會消失,它們仍然會有用嗎?這聽起來像是他們在textarea旁邊或鏈接的另一端可能會更好,因此在填寫時可供參考。 – Quentin 2013-04-08 09:27:29

+0

正確我可能會探討該選項,因爲佔位符不在ie9或更早版本中 – 2013-04-08 09:33:21

0

更好的方式來提供命中輸入要麼你可以使用佔位屬性也可以自定義提示。 您可以嘗試跟蹤代碼:

<textarea placeholder="text to be display as example" name="nameoftextarea"></textarea>