javascript - 如果通过reactjs解析,点击不起作用

标签 javascript reactjs

我编写了一段代码,需要首先 chop 文本,然后单击“阅读更多”,应显示全文。但点击时点击不起作用

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>

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

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

相关文章:

javascript - 计算表单中嵌套字段集的数量

javascript - 为 Flutter Web 应用添加 id 或 name 属性或其他标识方式?

html - 如何使用 Cypress 定位没有 id 属性的嵌套输入?

reactjs - 不使用 Immutability Helper 从嵌套数组中删除元素

reactjs - react : Ternary operation in JSX produces "Unexpected token" error

javascript - React 类旧的生命周期方法变成钩子(Hook)

asp.net - 如何使用 javascript 更改 asp.net 文本框的文本属性

javascript - 不变违规 : Text strings must be rendered within a <Text> component

javascript - 在 Amcharts 中动态改变 valueAxis

javascript - Reactjs - Formik 表单不会在按下返回键时触发提交