Javascript 如何更改文本框中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5683767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:26:49  来源:igfitidea点击:

How to change text in textbox

javascript

提问by norris1023

Ok this is wheat I have so far. IF someone could help me out. I had to change the color of the background when you click on a button. And I also have to Use document.getElementById('yourelementid') to both find the value of the textarea and to change the basic text created in the div. But I don't know how to do that i have been researching online. I think i am getting a little confused about where to put things at in here thanks.

好的,这是我目前拥有的小麦。如果有人可以帮助我。当你点击一个按钮时,我不得不改变背景的颜色。而且我还必须使用 document.getElementById('yourelementid') 来查找 textarea 的值并更改在 div 中创建的基本文本。但我不知道该怎么做,我一直在网上研究。我想我对把东西放在这里的位置有点困惑,谢谢。

Here is what I have so far....

这是我到目前为止所拥有的......

      <!DOCTYPE html PUBLIC "-//W3C//DTD Xhtml 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http:www.w3.org/1999/xhtml">
     <head>
     <title>DOM</title>
     <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />

    <script language="JavaScript">
     <!-- Begin
       function newbg(thecolor)
      {
     document.bgColor=thecolor;
      }

     //  End -->
     </script>
      <body>
    <h3>DOM Assignment Examples</h3>
     <div>
    <form>
      <h4>Change background color to:</h4>
     <input type="radio" value="White" onclick="newbg('white');">white<br/>
     <input type="radio" value="Blue" onclick="newbg('blue');">Blue<br />
     <input type="radio" value="Beige" onclick="newbg('Beige');">Beige<br />
     <input type="radio" value="Yellow" onclick="newbg('yellow');">Yellow<br />
    </form>
   </div>
   <br />
    <br />
    <br />
     <h4>add text to this box change the text below:</h4>
    <TEXTAREA NAME="" ROWS="10" COLS="40" onBlur="blurHandlerRouting">
    You will change this text
  </TEXTAREA> <br />
  <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
   </body>


  </html>

回答by njbair

The contents of a <textarea>can be manipulated using .html()in jQuery or .innerHTMLin vanilla JavaScript.

<textarea>可以使用.html()jQuery 或.innerHTMLvanilla JavaScript来操作a 的内容。

Your HTML should contain the id=""attribute:

您的 HTML 应包含以下id=""属性:

<textarea id="mytextarea">Text to be changed</textarea>

And the JavaScript:

和 JavaScript:

document.getElementById('mytextarea').innerHTML = "New Text";

回答by Galled

To change the background and the textarea you would try this:

要更改背景和文本区域,您可以尝试以下操作:

function = testResults(){
    document.getElementById("yourTextBoxId").style="background:red;"; //Change the background color to red.
    document.getElementById("yourTextAreaId").value="your another text for the textarea"
}