2013-01-07 184 views
0

刪除多個文本域文字我有這樣的代碼至極去除textarea的文本只在第一次點擊。它只能確定,直到我寫的第二textarea標籤至極爲:
<textarea id="textarea2" onfocus="checkOnFocus(this);" onblur="resetInitialText(this);">Your second message</textarea>只在第一次點擊

代碼:

<!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> 
<script type="text/javascript"> 
    var flag = false; 

    function setInitialText() { 
    var textarea = document.getElementById('textarea'); 
    if (textarea.value == "") { 
     textarea.value = text; 
    } 
    } 

    function checkOnFocus(textarea) { 
    if (!flag) textarea.value = ""; 
    flag = true; 
    } 

    function resetInitialText(textarea) { 
    if (textarea.value == "") { 
     textarea.value = text; 
     flag = false; 
    } 
    } 
</script> 
</head> 
<body onload="setInitialText();"> 
    <textarea id="textarea" onfocus="checkOnFocus(this);" onblur="resetInitialText(this);">Your first message</textarea> 
    <textarea id="textarea2" onfocus="checkOnFocus(this);" onblur="resetInitialText(this);">Your second message</textarea> 
</body> 
</html> 
+1

縮進你的代碼會傷害嗎? – PeeHaa

+0

你在控制檯中有任何錯誤嗎? – wakooka

+3

您對兩個textareas使用相同的全局「標誌」。爲什麼要避免全球國家的完美例子。 – Yoshi

回答

1

我終於找到了如何做我問:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title></title> 
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script> 
<script type='text/javascript'>//<![CDATA[ 

$(function() { 
    $("textarea").focus(function(event) { 

      // Erase text from inside textarea 
     $(this).text(""); 

      // Disable text erase 
     $(this).unbind(event); 
    }); 
}); 
//]]> 

</script> 


</head> 
<body> 
    <textarea rows="5" cols="30">Enter text here.</textarea> 
    <textarea rows="5" cols="30">Enter text here.</textarea> 
</body> 
</html> 
0

你只有一個(全局)標誌和兩個文字區域,因此由當時的第二個調用,該標誌已被設置爲true。考慮使用數組或字典類型結構來創建通用實現。一種選擇是在全球範圍內使用數組:

var textareasAlreadyHit = new Array(); 

然後每次時間:

function checkOnFocus(textarea) 

不是檢查的標誌,檢查是否數組包含textarea的ID。如果是這樣,則什麼都不要做。如果沒有,請從textarea中刪除文本,並將textarea的id添加到數組中。

0
function checkOnFocus(textarea){ 
if(!flag) 
textarea.value=""; 
flag=false; 
} 

function resetInitialText(textarea){ 
if(textarea.value==""){ 
textarea.value='text'; 
flag=false; 
} 

兩個變化在這裏

1> checkOnFocus()方法

flag=false; 

2> resetInitialText()方法

textarea.value='text';// make your own string to replace 
+0

當我嘗試修改textarea中輸入的文本時,該字段被再次清除... – CBuzatu

1

從你的回答來看,你真行使用jQuery,你可以試試下面的一個非常基本的佔位 - 溶液:

$('[data-placeholder]').each(function() { 
    var placeholder = $(this).data('placeholder'); 

    $(this).val(placeholder).on({ 
    focus: function() { 
     $(this).val(function (_, value) { 
     return value === placeholder ? '' : value; 
     }); 
    }, 

    blur: function() { 
     $(this).val(function (_, value) { 
     return value === '' ? placeholder : value; 
     }); 
    } 
    }); 
}); 

有:

<textarea data-placeholder="Your first message"></textarea> 
<textarea data-placeholder="Your second message"></textarea> 

http://jsbin.com/iyobiy/1/

相關問題