Javascript 更改 textarea 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5528223/
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
Changing value of textarea
提问by benone
I write this script to change the value of a textarea but I fail to do so. What's wrong with my code?
我编写此脚本来更改 textarea 的值,但我没有这样做。我的代码有什么问题?
<html>
<head>
<script type="text/javascript">
document.getElementsByName("status").innerHTML = "hi";
document.getElementsByName("status").title= "hi";
document.getElementsByName("status").placeholder= "hi";
</script>
</head>
<body>
<textarea placeholder="What's on your mind?" onfocus="window.UIComposer && UIComposer.focusInstance("c4d981e9a2c98b0483252333");" name="status" id="c4d981e9a2c98b0483252333_input" title="What's on your mind?" class="DOMControl_placeholder UIComposer_TextArea">What's on your mind?</textarea>
</body>
</html>
回答by David says reinstate Monica
Iif you try using:
如果您尝试使用:
var textarea = document.getElementById('c4d981e9a2c98b0483252333_input');
textarea.value = 'hi';
It should work.
它应该工作。
Otherwise, because of the way getElementsByName
works, you'd need to provide an index (zero-based) to the call to identify which of the textareas you want to work with:
否则,由于工作方式getElementsByName
,您需要为调用提供一个索引(从零开始)以标识您要使用的文本区域:
var textarea = document.getElementByName('status')[0]; // selects the first element of name 'status'
textarea.value = 'hi';
回答by Quentin
Two problems
两个问题
First, document.getElementsByName
returns a NodeList (which is like an array), not a single element.
首先,document.getElementsByName
返回一个 NodeList(就像一个数组),而不是单个元素。
Second, you do nothing to delay the execution of the JS until the element actually exists. So it won't find it anyway.
其次,您不会延迟 JS 的执行,直到元素实际存在。所以无论如何也找不到。
- Change to
document.getElementsByName("status")[0]
- Move the
<script>
element so it appears afterthe textarea.
- 改成
document.getElementsByName("status")[0]
- 移动
<script>
元素,使其出现在textarea 之后。
I wouldn't be comfortable with using innerHTML
to modify a form control either. I'd switch to value
instead.
我也不喜欢innerHTML
用来修改表单控件。我会改用value
。
回答by Quentin
<script type="text/javascript">
document.getElementsByName("status")[0].value = "hi";
</script>
回答by MuniR
put this script in the body
把这个脚本放在正文中
<script type="text/javascript">
document.getElementById("status").value= "hi";
</script>
change getElementsByName to getElementById
将 getElementsByName 更改为 getElementById
getElementsByName -> returns array of elements getElementById -> returns single control..
getElementsByName -> 返回元素数组 getElementById -> 返回单个控件..
then put the script in between body tags not in header.....
然后将脚本放在正文标记之间而不是标题中.....
回答by Farzin Zaker
<html>
<head>
</head>
<body>
<textarea placeholder="What's on your mind?" onfocus="window.UIComposer && UIComposer.focusInstance("c4d981e9a2c98b0483252333");" name="status" id="c4d981e9a2c98b0483252333_input" title="What's on your mind?" class="DOMControl_placeholder UIComposer_TextArea">What's on your mind?</textarea>
<script type="text/javascript">
document.getElementById("c4d981e9a2c98b0483252333_input").value = "hi";
document.getElementsByName("status")[0].value = "hi";
</script>
</body>
</html>