admin管理员组文章数量:1026989
I have a click function for a parent
element. I want to detect now if the part I clicked has the class "child"
$( ".parent" ).click(function() {
if ( $( this ).hasClass( "child" ) ) {
console.log("child");
}
});
.child{background-color:pink}
<script src=".1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
I have a click function for a parent
element. I want to detect now if the part I clicked has the class "child"
$( ".parent" ).click(function() {
if ( $( this ).hasClass( "child" ) ) {
console.log("child");
}
});
.child{background-color:pink}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
Share
Improve this question
edited Jan 11, 2018 at 15:56
Smern
19.1k22 gold badges77 silver badges93 bronze badges
asked Jan 11, 2018 at 15:46
peace_lovepeace_love
6,47114 gold badges85 silver badges184 bronze badges
1
-
2
Does the function only have to do something if the clicked element has the class
child
? If so then use event delegation and let jQuery do the work for you:$(".parent").on("click", ".child", function() { /* <this> is the clicked "child" element */ })
– Andreas Commented Jan 11, 2018 at 15:51
2 Answers
Reset to default 3access event.target
, this always references the original target that created the event.
in this case the event started with the .child
and bubbled up to .parent
which hit this listener... at this point, this
and event.currentTarget
will reference the .parent
element.. but target
will still reference the origin element, .child
.
$( ".parent" ).click(function(e) {
if ( $( e.target ).hasClass( "child" ) ) {
console.log("child");
}
});
JSFiddle Demo
Also, unless you have another reason to have the listener on .parent
, you could just add the listener directly to the child like this:
$( ".parent .child" ).click(function() {
console.log("child");
});
You can use event.target
to determine the original target of the click:
$(".parent").click(function(e) {
if ($(e.target).hasClass("child")) {
console.log("child");
}
});
.child {
background-color: pink
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
I have a click function for a parent
element. I want to detect now if the part I clicked has the class "child"
$( ".parent" ).click(function() {
if ( $( this ).hasClass( "child" ) ) {
console.log("child");
}
});
.child{background-color:pink}
<script src=".1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
I have a click function for a parent
element. I want to detect now if the part I clicked has the class "child"
$( ".parent" ).click(function() {
if ( $( this ).hasClass( "child" ) ) {
console.log("child");
}
});
.child{background-color:pink}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
Share
Improve this question
edited Jan 11, 2018 at 15:56
Smern
19.1k22 gold badges77 silver badges93 bronze badges
asked Jan 11, 2018 at 15:46
peace_lovepeace_love
6,47114 gold badges85 silver badges184 bronze badges
1
-
2
Does the function only have to do something if the clicked element has the class
child
? If so then use event delegation and let jQuery do the work for you:$(".parent").on("click", ".child", function() { /* <this> is the clicked "child" element */ })
– Andreas Commented Jan 11, 2018 at 15:51
2 Answers
Reset to default 3access event.target
, this always references the original target that created the event.
in this case the event started with the .child
and bubbled up to .parent
which hit this listener... at this point, this
and event.currentTarget
will reference the .parent
element.. but target
will still reference the origin element, .child
.
$( ".parent" ).click(function(e) {
if ( $( e.target ).hasClass( "child" ) ) {
console.log("child");
}
});
JSFiddle Demo
Also, unless you have another reason to have the listener on .parent
, you could just add the listener directly to the child like this:
$( ".parent .child" ).click(function() {
console.log("child");
});
You can use event.target
to determine the original target of the click:
$(".parent").click(function(e) {
if ($(e.target).hasClass("child")) {
console.log("child");
}
});
.child {
background-color: pink
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr class="parent">
<th>Firstname</th>
<th>Lastname</th>
<th class="child">Age</th>
</tr>
</table>
本文标签: javascriptHow can I detect a child element by clicking on a parent elementStack Overflow
版权声明:本文标题:javascript - How can I detect a child element by clicking on a parent element? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659479a2161800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论