簡體   English   中英

為什么textarea.value返回“未定義”? (使用本機Javascript)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM