获得HTML的价值

时间:2020-02-22 11:24:02

标签: 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 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;

}

// ...get the textarea element's current value
  var data = textareaElement.value;

  // ...make a text item using the value
  var item = makeItem("text", data);
  // ...store the item in local storage using key
  localStorage.setItem(key, item);
  // Connect the event listener to the textarea element:
  textareaElement.addEventListener('onblur', addTextEntry);

}   

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]

1 个答案:

答案 0 :(得分:2)

“ textareaElements”不是复数形式,

var data = textareaElements.value;

这是正确的形式:

var data = textareaElement.value;