admin管理员组文章数量:1293315
,js,output
JSX
let MY = React.createClass({
sendMsg : function(e){
alert($(e.target).attr('data-id'));
//sendMsgButton = ??
},
render: function() {
return (
<button is class = "send_msg"
data-id = "10"
onClick = {
this.sendMsg
} >
Send Message
<span> INSIDE SPAN </span> <span className = "sendMsgIcon" > ICON </span> </button>
);
}
});
ReactDOM.render(
<MY />,
document.getElementById("container")
);
Whenever im clicking the button sendMsg
i want the button element inside sendMsg
function.
But whenever im clicking a span or child element of the button e.target
returns the span/child element instead of button itself ( i know that's what e.target does)
But how do i get the element which was clicked ?
In Jquery its possible using
$('.sendMsg').click(function(){
let sendMsgButton = $(this);
});
How do i get the exact element ?
https://jsbin./diyenakife/edit?html,js,output
JSX
let MY = React.createClass({
sendMsg : function(e){
alert($(e.target).attr('data-id'));
//sendMsgButton = ??
},
render: function() {
return (
<button is class = "send_msg"
data-id = "10"
onClick = {
this.sendMsg
} >
Send Message
<span> INSIDE SPAN </span> <span className = "sendMsgIcon" > ICON </span> </button>
);
}
});
ReactDOM.render(
<MY />,
document.getElementById("container")
);
Whenever im clicking the button sendMsg
i want the button element inside sendMsg
function.
But whenever im clicking a span or child element of the button e.target
returns the span/child element instead of button itself ( i know that's what e.target does)
But how do i get the element which was clicked ?
In Jquery its possible using
$('.sendMsg').click(function(){
let sendMsgButton = $(this);
});
How do i get the exact element ?
Share Improve this question asked Feb 24, 2017 at 9:30 IMOBAMAIMOBAMA 1,9273 gold badges15 silver badges13 bronze badges 03 Answers
Reset to default 7You should use e.currentTarget
instead of e.target
Example:
sendMsg : function(e){
alert($(e.currentTarget).attr('data-id'));
//sendMsgButton = ??
}
Hope this helps!
Use react refs
so you can avoid using Jquery and DOM selectors.
https://jsbin./senevitaji/1/edit?html,js,output
let MY = React.createClass({
sendMsg : function(e){
alert(this.button.getAttribute('data-id'));
//sendMsgButton = ??
},
render: function() {
return (
<button is class = "send_msg"
data-id = "10"
ref={(button) => { this.button = button; }}
onClick = {
this.sendMsg
} >
Send Message
<span> INSIDE SPAN </span> <span className = "sendMsgIcon" > ICON </span> </button>
);
}
});
Unrelated to your question, but this is how I would do this to avoid using jQuery & reading data form DOM.
https://jsbin./zubefepipe/1/edit?html,js,output
let Button = React.createClass({
_onClick: function(e){
e.preventDefault();
this.props.onClick(e, this)
},
getSomeAttr: function(){
return this.props.someAttr;
},
render : function() {
return (
<button onClick={this._onClick}>
Send Message
<span> INSIDE SPAN </span>
<span className = "sendMsgIcon"> ICON </span>
</button>
);
}
});
let MY = React.createClass({
sendMsg : function(e, btn){
alert(btn.getSomeAttr());
},
render: function() {
return (
<Button someAttr="10" onClick={this.sendMsg}/>
);
}
});
ReactDOM.render(
<MY />,
document.getElementById("container")
);
本文标签: javascriptReact Js get the element which was clickedStack Overflow
版权声明:本文标题:javascript - React Js get the element which was clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741566439a2385752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论