发生onblur时如何获取textarea的当前编辑值?

我想在 onblur 发生时获取我的 textarea 元素的当前编辑后的值。尽管已经改变了内容,DOM 仍然给我原始内容。例如...


<!DOCTYPE html>

<html>


<head>

  <meta charset="utf-8" />

  <title>blur updates</title>

  <script>

    viewContent = function(id) {

      console.log(document.getElementById(id).innerHTML);

    };

  </script>

</head>


<body>

  <p>

    Edit one, then click in two. Does the value change in the console?

    <br/> No, it does not.

  </p>

  <br/>

  <form>

    <textarea id="1" onblur="viewContent('1')" cols="24" rows="4">one</textarea>

    <textarea id="2" onblur="viewContent('2')" cols="24" rows="4">two</textarea>

  </form>

</body>


</html>

对 textarea 的任何编辑都不会反映在控制台中。如何获取当前内容?


偶然的你
浏览 467回答 1
1回答

qq_花开花谢_0

使用.value代替.innerHTML<!DOCTYPE html><html><head>&nbsp; <meta charset="utf-8" />&nbsp; <title>blur updates</title>&nbsp; <script>&nbsp; &nbsp; viewContent = function(id) {&nbsp; &nbsp; &nbsp; console.log(document.getElementById(id).value);&nbsp; &nbsp; };&nbsp; </script></head><body>&nbsp; <p>&nbsp; &nbsp; Edit one, then click in two. Does the value change in the console?&nbsp; &nbsp; <br/> No, it does not.&nbsp; </p>&nbsp; <br/>&nbsp; <form>&nbsp; &nbsp; <textarea id="1" onblur="viewContent('1')" cols="24" rows="4">one</textarea>&nbsp; &nbsp; <textarea id="2" onblur="viewContent('2')" cols="24" rows="4">two</textarea>&nbsp; </form></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript