简体   繁体   English

在 javascript 中将变量打印到文本区域而不是控制台

[英]print variable to text area instead of console in javascript

little confused on this.对此有点困惑。 I currently have some variables printing to my console when a button is pressed, but instead I'd like for them to print to a text area (on a single line) instead.当按下按钮时,我目前有一些变量打印到我的控制台,但我希望它们打印到文本区域(在单行上)。

Here's my code这是我的代码

 const freq = palindromes.map(palindrome => ({ palindrome, freq: frequency(palindrome) })); console.log('all sentences:', sentences); console.log('only palindromes:', palindromes); console.log(freq);
 <textarea id="output"></textarea>

Which just in the console shows-就在控制台中显示-

[{
  freq: {
   a: 2,
   c: 2,
   e: 1,
   r: 2
},
  palindrome: "racecar"
}]

Instead of this I want it to display like [{freq: { a: 2, c: 2, e: 1,r: 2}, palindrome: "racecar"}] inside the text area I defined in the html. Instead of this I want it to display like [{freq: { a: 2, c: 2, e: 1,r: 2}, palindrome: "racecar"}] inside the text area I defined in the html. How do I go about accomplishing this?我该如何完成这个任务?

Thanks!谢谢!

document.getElementById('output').value = JSON.stringify('your variable')

There are many ways to implement what you are trying to do assuming I am interpreting your question correctly.假设我正确解释了您的问题,有很多方法可以实现您正在尝试做的事情。 It sounds like you want a text field, or in this case through the use of the textarea tag, to hold some string of characters rather then being empty?听起来你想要一个文本字段,或者在这种情况下通过使用 textarea 标签来保存一些字符串而不是空的? This can be done once at start up or dynamically through a button or something during run time.这可以在启动时完成一次,也可以在运行时通过按钮或其他东西动态完成。 The code needed looks something like this:所需的代码如下所示:

<textarea id="output">
default text.
</textarea>

<button type="button" onclick="upDateOutput()">Click to update text area</button>

<script>
function upDateOutput() {
  document.getElementById("output").value = "This is a new string.";
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM