我编写了一段代码,需要首先 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/