[英]How to process onClick and not href <a>
So I'm using a jquery plugin and for it to work properly I have to have my book now button (styled to look like a button) be inside an anchor tag. 所以我正在使用一个jquery插件,为了它正常工作,我必须让我的书现在按钮(样式看起来像一个按钮)在一个锚标签内。 My react component looks like this:
我的反应组件如下所示:
export default class Header extends React.Component {
constructor(props){
super(props);
this.state ={
showModal: false,
};
this.openModal = this.openModal.bind(this);
this.exitModal = this.exitModal.bind(this);
}
openModal(){
console.log('hello');
this.setState(()=> ({showModal: true}));
return false;
}
exitModal(){
this.setState(()=> ({showModal: false}));
}
render(){
return (
...
<li>
<a id="navbar-book" href="#" onClick={this.openModal} className="book-button"><span>Book Now</span></a>
</li>
...
<BookModal
showModal={this.state.showModal}
exitModal={this.exitModal}
/>
)
}
}; };
I don't want to have an href but if I don't have it my dropotron plugin won't work on it. 我不想要一个href,但如果我没有它,我的dropotron插件将无法使用它。 I've already tried to return false from the function in hopes that href wouldn't run.
我已经尝试从函数中返回false,希望href不会运行。 All I need it to do is run the modal.
我需要做的就是运行模态。
You can use a button like this: 你可以使用这样的按钮:
<button onClick={this.openModal}>Book Now</button>
But if you really must use a link you must use preventDefault on the click event like this: 但是如果你真的必须使用链接,你必须对click事件使用preventDefault ,如下所示:
openModal(event){
event.preventDefault();
this.setState(()=> ({showModal: true}));
return false;
}
如何用按钮替换它?
<button onClick={this.openModal}>Book Now</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.