[英]Replacing text inside a textarea box
我有以下內容:
<textarea name="comment"><p>Good job.</p></textarea>
使用JavaScript,我想用“這是我給作業評分的方式:”代替“好工作”。
我對替換字符串還不滿意。
您的用戶個人資料上有jquery。 所以這是使用它的答案:
$('textarea[name="comment"]').text("<p>Here's how I graded your assignment:</p>");
我建議將一個類添加到textarea並通過該類選擇它。 上面的代碼假定您在頁面上沒有名稱為'comment'的其他文本區域。
這樣會更好:
<textarea name="comment" class="some-better-name-here"><p>Good job.</p></textarea>
$('textarea.some-better-name-here').text("<p>Here's how I graded your assignment:</p>");
它在起作用: http : //jsfiddle.net/MCVbg/
一個純Javascript解決方案:
var myTextArea = document.getElementsByName("comment")[0];
myTextArea.value = myTextArea.value.replace("Good job", "Here's how I graded your assignment:");
最好提供您的textarea
和id並使用getElementById
選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.