Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从React ContentEditable中提取没有HTML实体编码的文本?_Javascript_Html_Reactjs_Contenteditable - Fatal编程技术网

Javascript 如何从React ContentEditable中提取没有HTML实体编码的文本?

Javascript 如何从React ContentEditable中提取没有HTML实体编码的文本?,javascript,html,reactjs,contenteditable,Javascript,Html,Reactjs,Contenteditable,我有一个基于react-react-contenteditable的文本区域。当用户写东西时 例如“A&B”,即e.target.value将其作为“A&B”返回如何提取与原始文本相同的内容? onChange(e) { const val = e.target.value; this.setState({ value: val }); return this.props.onChange(e, this.props.value, val); }

我有一个基于react-react-contenteditable的文本区域。当用户写东西时

例如“A&B”,即e.target.value将其作为“A&B”返回如何提取与原始文本相同的内容?

onChange(e) {
    const val = e.target.value;

    this.setState({
        value: val
    });
    return this.props.onChange(e, this.props.value, val);
}

onPaste(e) {
    e.preventDefault();
    const text = e.clipboardData.getData("text/plain");

    document.execCommand("insertText", false, text);
    return true;
}

render() {
    return (
        <ContentEditable
            className={this.state.className}
            html={this.state.value}
            placeholder={this.props.placeholder}
            disabled={false}
            onPaste={this.onPaste}
            onBlur={::this.onBlur}
            onFocus={::this.onFocus}
            onChange={::this.onChange}
        />
    );
}
onChange(e){
const val=e.target.value;
这是我的国家({
值:val
});
返回this.props.onChange(e,this.props.value,val);
}
onPaste(e){
e、 预防默认值();
const text=e.clipboardData.getData(“text/plain”);
document.execCommand(“insertText”,false,text);
返回true;
}
render(){
返回(
);
}

此包用于处理HTML(对于富文本),因此返回的值是HTML。但是如果您想要解析文本(缺少富文本),可以直接从
contenteditable
div中读取文本

该包提供了一个很好的界面,可以使用prop
innerRef
将ref转发到
contenteditable
div。然后,您可以随时使用
innerRef.current.innerText
访问解析的文本

constructor() {
    this.editableRef = React.createRef(null);
}

onChange(e) {
    console.log(this.editableRef.current.innerText);

    const val = e.target.value;
    this.setState({ value: val });
    return this.props.onChange(e, this.props.value, val);
}


render() {
    return (
        <ContentEditable
            innerRef={this.editableRef}
            html={this.state.value}
            onChange={::this.onChange}
        />
    );
}
constructor(){
this.editableRef=React.createRef(null);
}
onChange(e){
log(this.editableRef.current.innerText);
const val=e.target.value;
this.setState({value:val});
返回this.props.onChange(e,this.props.value,val);
}
render(){
返回(
);
}

此包用于处理HTML(对于富文本),因此返回的值是HTML。但是如果您想要解析文本(缺少富文本),可以直接从
contenteditable
div中读取文本

该包提供了一个很好的界面,可以使用prop
innerRef
将ref转发到
contenteditable
div。然后,您可以随时使用
innerRef.current.innerText
访问解析的文本

constructor() {
    this.editableRef = React.createRef(null);
}

onChange(e) {
    console.log(this.editableRef.current.innerText);

    const val = e.target.value;
    this.setState({ value: val });
    return this.props.onChange(e, this.props.value, val);
}


render() {
    return (
        <ContentEditable
            innerRef={this.editableRef}
            html={this.state.value}
            onChange={::this.onChange}
        />
    );
}
constructor(){
this.editableRef=React.createRef(null);
}
onChange(e){
log(this.editableRef.current.innerText);
const val=e.target.value;
this.setState({value:val});
返回this.props.onChange(e,this.props.value,val);
}
render(){
返回(
);
}