开发者问题收集

获取 HTML 值

2020-02-22
131

我正在学习 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个回答

'textareaElements' 不是复数,因为您在此处看到的是:

var data = textareaElements.value;

这是正确的形式:

var data = textareaElement.value;
Sudhanshu Garg
2020-02-22