我想在 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 的任何编辑都不会反映在控制台中。如何获取当前内容?
1 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
使用.value代替.innerHTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>blur updates</title>
<script>
viewContent = function(id) {
console.log(document.getElementById(id).value);
};
</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>
添加回答
举报
0/150
提交
取消