Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取HTML的值_Javascript_Html_Dom - Fatal编程技术网

Javascript 获取HTML的值

Javascript 获取HTML的值,javascript,html,dom,Javascript,Html,Dom,我正在学习JavaScript,在尝试获取textareaElement的HTML值时遇到了问题。网上有很多信息,由于所有可用的信息,这使得它更加混乱。我理解DOM背后的想法,但不确定如何编写代码。我还尝试使用添加事件侦听器将数据存储在本地存储中,但没有任何运气 // Add a text entry to the page function addTextEntry(key, text, isNewEntry) { // Create a textarea element to edit

我正在学习JavaScript,在尝试获取textareaElement的HTML值时遇到了问题。网上有很多信息,由于所有可用的信息,这使得它更加混乱。我理解DOM背后的想法,但不确定如何编写代码。我还尝试使用添加事件侦听器将数据存储在本地存储中,但没有任何运气

// Add a text entry to the page
function addTextEntry(key, text, isNewEntry) {
  // Create a textarea element to edit the entry
  var textareaElement = document.createElement("TEXTAREA");
  textareaElement.rows = 5;
  textareaElement.placeholder = "(new entry)";


  // Set the textarea's value to the given text (if any)
  textareaElement.value = text;

  // Add a section to the page containing the textarea
  addSection(key, textareaElement);

  // If this is a new entry (added by the user clicking a button)
  // move the focus to the textarea to encourage typing
  if (isNewEntry) {
    textareaElement.focus();

// Get HTML input values
var data = textareaElement.value;
}

HTML是:

<section id="text" class="button">
    <button type="button">Add entry</button>
</section>
<section id="image" class="button">
    <button type="button">Add photo</button>
    <input type="file" accept="image/*" />
</section>

添加条目
添加照片

[HTML][1]

'textareaElements'不是这里的复数:

var data = textareaElements.value;
这是正确的形式:

var data = textareaElement.value;

所提供的示例缺少两个右大括号,不清楚您究竟在哪里读取
var data=textareaElements.value。它是在函数内部还是代码中的其他地方?另一件事是,它不能包含HTML,它的内容只是纯文本。而
textareaElements
(复数)来自哪里?为什么不能使用jquery?搜索有关它的文档,可以使用val()。简单快速。
var data = textareaElement.value;