我已经编写了代码,需要先截断文本,然后再单击阅读更多内容,然后显示全文。但点击后点击不起作用



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>





将函数放在组件外部,然后不带此调用。

关于javascript - 通过reactjs解析时单击不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42199548/

10-09 21:45