2013-03-18 92 views
0

如何在點擊時將我的textarea內容更改爲空?使用jquery更改點擊textarea內容

下面是代碼:

http://jsfiddle.net/QMfyP/

<textarea name="adventage" style="width:400px; border-radius: 3px; border-left: 4px solid #6C3" id="adventage" cols="45" rows="5">Share with everyone what are the advantages of this vehicle. </textarea> 

的想法是在點擊這個區域,這將是空的打字新文本。

+0

'placeHolder'屬性是最好和簡單的方法來做到這一點。 – SachinGutte 2013-03-18 14:41:35

回答

4

您可以使用placeholder屬性

<textarea placeholder='Share with everyone what are the advantages of this vehicle.'></textarea> 

這是標準和最簡單的方式獲得該功能(不涉及JavaScript中,你可以看到它在這個頁面的頂部計算器的搜索文本框的工作!)。

它不會在舊的瀏覽器,但(IE9及以上)的工作,如果你需要在這些瀏覽器中工作,你可以檢查此question

+0

用於提及佔位符屬性的+1 – 2013-03-18 14:42:36

+1

好的建議,但IE 9和更早版本不支持「佔位符」。如果瀏覽器的兼容性對你來說不是問題,這是完美的,否則使用@adamb的答案。 – Alex 2013-03-18 14:43:17

0
$('YOURSELECTOR').click(function(e) { 
    $('#adventage').empty(); 
}); 
1

給textarea的一個獨特id(因爲你可能贏不希望將此應用於所有textareas),例如thetextarea

$("#thetextarea").click(function(){ 
    $("#thetextarea").empty(); 
}); 

這裏的工作jsFiddle

0

http://jsfiddle.net/QMfyP/6/

HTML

<textarea class="my_textarea" name="adventage" id="adventage" cols="45" rows="5">Share with everyone what are the advantages of this vehicle. </textarea> 

JQUERY

$("#adventage").click(function() { 
    $(this).empty(); 
}); 

CSS

#adventage { width:400px; border-radius: 3px; border-left: 4px solid #6C3; } 
0
$(document).ready(function(){ 
    $("textarea[name='adventage']").click(function({ 
     $(this).innerHTML(""); 
    })) 
}) 

這應該工作!

0

試試這個。

第一次佔位符的工作。但如果你有一些文字已經在那裏(in case of editing)做一些scripting

$(document).ready(function(){ 
     $('#adventage').click(function(){ 
      $(this).val(''); 
     }) 

});