简体   繁体   English

更改后如何获取textarea的textContent?

[英]how to get textContent of textarea after change?

I have a textarea tag. 我有一个textarea标签。 I want to be able to get its textContent after textContent changed? 我希望在textContent更改后能够获取其textContent吗?

<textarea id="xxx"> sameple text </textarea> // before
<textarea id="xxx"> sameple text and new text </textarea> // after

I want listen even when the textarea 's content has changed. 即使textarea的内容已更改,我也想听。

you can put your script as 你可以把你的脚本作为

$(document).ready(function(){
  $("#xxx").change(function(){
     alert($("#xxx").val());
  });
});

if you put $("#xxx").html() it will give you before text but if you keep $("#xxx").val() it will give you changed value. 如果您放置$("#xxx").html() ,它将在文本之前显示给您,但是如果您保留$("#xxx").val() ,则将为您更改值。

You will need to use onkeyup and onchange for this. 为此,您将需要使用onkeyup和onchange。 The onchange will prevent context-menu pasting, and the onkeyup will fire for every keystroke. onchange将阻止上下文菜单粘贴,并且onkeyup将为每次击键触发。

$(document).ready(function(){
       $(document).on('change','#xxx',function(){
             var newtext = $(this).text();
       });
});

Use input event. 使用input事件。

The DOM input event is fired synchronously when the value of an <input> or <textarea> element is changed. 更改<input><textarea>元素的值时,将同步触发DOM input event

 $('#xxx').on('input', function() { console.log(this.value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <textarea id="xxx">sameple text</textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM