簡體   English   中英

如何在div元素中獲取textarea值?

[英]How to get the textarea value in a div element?

我想要在textarea鍵入的text在div元素中typed id text ,我該怎么辦?

<script> 
function field()
{ 

var txt = document.getElementById("text").value;
if (txt.length > 0){
document.getElementById("typed").value = txt; 

}

}

</script>

身體

<input type="text" id="text"></input>

<br>
<b> You Typed : <div id="typed"></div> </b>
<br>
<input type="button" value="Submit" onclick="field()">

.value用於輸入元素。 要將某些內容放入DIV ,請使用.innerText

document.getElementById("typed").innerText = txt; 

帶有<input> DEMO

帶有<textarea> DEMO

如下更改功能

function field()
{ 
  var txt = document.getElementById("text").value;
  if (txt.length > 0)
  {
     document.getElementById("typed").innerText= txt; 
  }
}

你可以這樣:

    <html>
<head>

<script> 
function field()
{ 

var txt = document.getElementById("text").value;
if (txt.length > 0){
document.getElementById("typed").innerText = txt; 
console.log(txt);
}

}

</script>

</head>

<body>
<input type="text" id="text"></input>

<br>
<b> You Typed : <div id="typed"></div> </b>
<br>
<input type="button" value="Submit" onclick="field()">
</body>

</html>
document.getElementById("typed").innerHtml = document.getElementById("text").value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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