获取HTML值

问题描述 投票:0回答:1

[enter image description here Im正在学习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>
javascript html dom
1个回答
0
投票

请检查此行

var data = textareaElements.value;

这里应该是textareaElement而不是复数形式。

© www.soinside.com 2019 - 2024. All rights reserved.