[英]How to change the Content of a <textarea> with JavaScript
How would I change the content of a <textarea>
element with JavaScript?我如何使用 JavaScript 更改<textarea>
元素的内容?
I want to make it empty.我想让它空着。
Like this:像这样:
document.getElementById('myTextarea').value = '';
or like this in jQuery:或者像这样在 jQuery 中:
$('#myTextarea').val('');
Where you have你在哪里
<textarea id="myTextarea" name="something">This text gets removed</textarea>
For all the downvoters and non-believers:对于所有反对者和非信徒:
Here's the MSDN reference这是 MSDN 参考
value Property: Retrieves or sets the text in the entry field of the textArea element. value 属性:检索或设置 textArea 元素的输入字段中的文本。
Here's the MDN reference 这是 MDN 参考
value DOMString The raw value contained in the control. value DOMString 控件中包含的原始值。
If you can use jQuery, and I highly recommend you do, you would simply do如果您可以使用 jQuery,并且我强烈建议您使用 jQuery,那么您只需使用
$('#myTextArea').val('');
Otherwise, it is browser dependent.否则,它依赖于浏览器。 Assuming you have假设你有
var myTextArea = document.getElementById('myTextArea');
In most browsers you do在大多数浏览器中,您会这样做
myTextArea.innerHTML = '';
But in Firefox, you do但是在 Firefox 中,你可以
myTextArea.innerText = '';
Figuring out what browser the user is using is left as an exercise for the reader.弄清楚用户使用的浏览器是留给读者的练习。 Unless you use jQuery, of course ;)除非你使用jQuery,当然;)
Edit : I take that back.编辑:我收回。 Looks like support for .innerHTML on textarea's has improved.看起来对 textarea 的 .innerHTML 的支持有所改进。 I tested in Chrome, Firefox and Internet Explorer, all of them cleared the textarea correctly.我在 Chrome、Firefox 和 Internet Explorer 中进行了测试,它们都正确地清除了 textarea。
Edit 2 : And I just checked, if you use .val('') in jQuery, it just sets the .value property for textarea's.编辑 2 :我刚刚检查过,如果您在 jQuery 中使用 .val(''),它只会为 textarea 设置 .value 属性。 So .value should be fine.所以 .value 应该没问题。
Although many correct answers have already been given, the classical (read non-DOM) approach would be like this:尽管已经给出了许多正确答案,但经典的(读取非 DOM)方法是这样的:
document.forms['yourform']['yourtextarea'].value = 'yourvalue';
where in the HTML your textarea is nested somewhere in a form like this:在 HTML 中,您的 textarea 以如下形式嵌套在某处:
<form name="yourform">
<textarea name="yourtextarea" rows="10" cols="60"></textarea>
</form>
And as it happens, that would work with Netscape Navigator 4 and Internet Explorer 3 too.碰巧的是,这也适用于Netscape Navigator 4 和 Internet Explorer 3。 And, not unimportant, Internet Explorer on mobile devices.而且,并非不重要的是,移动设备上的 Internet Explorer。
If it's jQuery...如果是 jQuery...
$("#myText").val('');
or要么
document.getElementById('myText').value = '';
Reference: Text Area Object参考: 文本区域对象
Put the textarea to a form, naming them, and just use the DOM objects easily, like this:将 textarea 放入表单中,命名它们,然后轻松使用 DOM 对象,如下所示:
<body onload="form1.box.value = 'Welcome!'">
<form name="form1">
<textarea name="box"></textarea>
</form>
</body>
Here is a simple vanilla JS example that changes the textarea content on a button click.这是一个简单的 vanilla JS 示例,它在单击按钮时更改文本区域内容。
const button = document.querySelector("#button"); const messageBox = document.querySelector("#message"); button.addEventListener("click", ()=>{ messageBox.innerText = "Please type your message here." });
<h1>TextArea Examplw with JavaScript</h1> <textarea id="message"></textarea> <button id="button">Click to Change</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.