单击“如果通过 ReactJS 解析则不起作用”



我已经编写了一个代码,需要首先截断文本,然后单击阅读更多,应显示全文。 但是单击时单击不起作用

class DC extends React.Component {
    constructor(props) {
      super(props);
    }
    _parseText(text, flag) {
      if (text.length > 200 && flag === true) {
        return text.substr(0, 200) + ' <a onClick={this._parseText(text,false)}>Read More...</a>';
      } else {
        return text;
      }
    }
    render() {
        return ( 
          <p dangerouslySetInnerHTML={{__html:this._parseText('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', true)}}></p>)
}
}
        
ReactDOM.render(<DC/>, document.getElementById('test'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="test">
</div>

    var _parseText=(text, flag)=> {
      console.log(flag)
      if (text.length > 200 && flag == true) {
        return text.substr(0, 200) + ' <a onClick={_parseText(text,false)}>Read More...</a>';
      } else {
        return text;
      }
    }
class DC extends React.Component {
    constructor(props) {
      super(props);
    }
    render() {
        return ( 
          <p dangerouslySetInnerHTML={{__html:_parseText('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', true)}}></p>)
}
}
        
ReactDOM.render(<DC/>, document.getElementById('test'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="test">
</div>

将函数放在组件外部,并在没有此功能的情况下调用它。

最新更新