[英]Why is textarea.value returning “undefined”? (using Native Javascript)
这真的很简单,但我无法弄清楚什么是错误的。
我得到了一个div
,一个textarea
,一个button
。
我最近想使用“ Native-Javascript”,因为我一直在使用jQuery
,因此我想在此基本功能上使用Native。
目的是将enter code here
的内容从enter code here
textarea转移到div
并在click
button
时在console
记录相同的内容。
查看我的代码:
Javascript:
function text_func (){
var btn = document.getElementsByTagName('button'),
div_out = document.getElementsByTagName('div');
txt_a = document.getElementsByTagName('textarea');
console.log(txt_a.value);// This Logs undefined :: WHY?
//return;
btn[0].addEventListener('click',btn_click,false);
function btn_click(){
console.log(txt_a.value)// This also logs undefined.. Why again?
div_out.innerHTML = txt_a.value;
}
}
window.onload = function(){
text_func();
}
HTML:
<div id="myDiv" style="width:300px; border:1px solid #ccc; height:100px; padding:10px;"></div>
<textarea style="width:300px; border:1px solid #ccc; height:100px; padding:10px;"></textarea>
<button>Go</button>
我在做什么错?....
参见Fiddle: http : //jsfiddle.net/3urm9/任何建议均受到高度赞赏。
你可以试试:
console.log(txt_a[0].value)
注意[0]
getElementsByTagName
这将返回一个元素数组 (实际上是一个NodeList)。
数组没有value
属性。
您可能想要从数组中获取元素之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.