admin管理员组文章数量:1406178
Our <a>
tag opens a custom pdf viewer in our application by providing a function to the onClick
property.
We want to provide a href for accessibility, so users can rightclick -> open in new tab, control+click or what else they fancy.
However, when the user clicks the <a>
tag, we don't want to actually open the link, instead our onClick
function should handle the event.
currently we have somthing like this:
render(){
return (<a href="www.stackoverflow" onClick={(e)=>this.linkClick(e)} >click here</a>)
}
linkClick(e) {
e.stopPropagation(); // does not stop the link from opening
console.log("link clicked");
return false; // does not stop the link from opening
}
In plain js this can be done by returning false on the onClick function.
We've also tried adding stopPropagation()
to no avail.
Some react users add href="javascript: void(0)"
while this does disable the link, it does not meet our accessibility needs.
Our <a>
tag opens a custom pdf viewer in our application by providing a function to the onClick
property.
We want to provide a href for accessibility, so users can rightclick -> open in new tab, control+click or what else they fancy.
However, when the user clicks the <a>
tag, we don't want to actually open the link, instead our onClick
function should handle the event.
currently we have somthing like this:
render(){
return (<a href="www.stackoverflow." onClick={(e)=>this.linkClick(e)} >click here</a>)
}
linkClick(e) {
e.stopPropagation(); // does not stop the link from opening
console.log("link clicked");
return false; // does not stop the link from opening
}
In plain js this can be done by returning false on the onClick function.
We've also tried adding stopPropagation()
to no avail.
Some react users add href="javascript: void(0)"
while this does disable the link, it does not meet our accessibility needs.
1 Answer
Reset to default 10Use preventDefault
. Returning false
has been deprecated since React 0.12.
linkClick(e) {
e.preventDefault();
console.log("link clicked");
}
本文标签: javascriptDon39t open href while an onClick is specified (in react js)Stack Overflow
版权声明:本文标题:javascript - Don't open href while an onClick is specified (in react js) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744969618a2635155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论