反应:在contentEditable div中获取文本的值 [英] React: Getting the value of text inside a contentEditable div

查看:502
本文介绍了反应:在contentEditable div中获取文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 contentEditable div,并希望在调用submit时获取其中的文本.我尝试了 this.refs.textarea.value.trim(),但这似乎不起作用.我的代码如下所示,在渲染返回中

I am using a contentEditable div and want to get the text inside it when a submit is called. I tried this.refs.textarea.value.trim() but that doesn't seem to work. My code is as follows in the render return

<div><form onSubmit={this.handleSubmit} id="noter-save-form" method="POST">

<div id="noter-text-area" contentEditable="true" ref="textarea">{value}</div>

handleSubmit 函数.

handleSubmit: function(e) { 
  e.preventDefault();
  var text = this.refs.textarea.value.trim();
  alert(text);
  this.saveFile(text);
},

感谢您的帮助.我只是想将text变量替换为可编辑div中的实际变量.

Thanks for the help. I am just looking to replace the text variable with what is actually inside the editable div.

推荐答案

如果它是div,则是否可编辑并应为 this.refs.textarea.innerHTML ,如果您只是想要文本 this.refs.textarea.innerText

if it's a div it doesn't matter if it's editable and should be this.refs.textarea.innerHTML and if you just want text this.refs.textarea.innerText

这篇关于反应:在contentEditable div中获取文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆