[英]Register click event to children of given div / a with class
I have the following HTML
: 我有以下
HTML
:
<div class="sidenav-contact__types">
<div class="sidenav-contact__type js-sidenav-contact__type" data-contact-type="call-me-back">
<figure>
<svg class="sidenav-contact__icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/etc/designs/site/img/icons.svg#caller-inverse"></use></svg>
</figure>
<h5>Call me back</h5>
<p>Talk to a Representative about our range.</p>
</div>
<div class="sidenav-contact__type js-sidenav-contact__type" data-contact-type="contact-us">
<figure>
<svg class="sidenav-contact__icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/etc/designs/site/img/icons.svg#info-inverse"></use></svg>
</figure>
<h5>Contact Us</h5>
<p>For FAQs, feedback, suggestions or media.</p>
</div>
<a class="sidenav-contact__type" href="http://site" data-contact-type="new button">
<figure>
<svg class="sidenav-contact__icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/etc/designs/site/img/icons.svg#recall-inverse"></use></svg>
</figure>
<h5>Information</h5>
<p>vehicle is affected.</p>
</a>
</div>
What I'm trying to do is detect when a user clicks inside a div
or a
with the given class sidenav-contact__type
, when clicked I then need to retrieve the data-contact-type
value. 我正在试图做的是,当用户点击一个内部检测
div
或a
给定类sidenav-contact__type
,当点击然后我需要检索data-contact-type
值。
Now I could of course target this class in general, but when I click on the h5
, p
, figure
or svg
the script doesn't run due to none of those elements having the class sidenav-contact__type
现在,我当然可以大体上针对该类了,但是当我单击
h5
, p
, figure
或svg
,脚本不会运行,因为这些元素都不具有sidenav-contact__type
类
The javascript I currently have is: 我目前拥有的javascript是:
var myElements = document.getElementsByClassName('sidenav-contact__type');
for(var i = 0; i < myElements.length; i++){
myElements[i].addEventListener('click', function(){
console.log('here');
//get data-contact-type from div or a
});
}
Can a javascript
guru please show me how I can register a click event on the children elements of the parent element that has the class sidenav-contact__types
javascript
大师能否向我展示如何在具有sidenav-contact__types
类的父元素的子元素上注册click事件
note: has to be in javascript
注意:必须在
javascript
try this it should work 试试这个应该工作
var myElements = document.querySelectorAll(".sidenav-contact__type, .sidenav-contact__type *"); //select every class element and it's childs
for(var i in myElements){
myElements[i].addEventListener('click', function(){
console.log('here');
//get data-contact-type from div or a
});
}
This way you can retrieve the data contact-type
attribute. 这样,您可以检索数据
contact-type
属性。 Note the camelcase notation of contactType
. 请注意
contactType
表示法。
var myElements = document.getElementsByClassName('sidenav-contact__type');
for(var i = 0; i < myElements.length; i++){
myElements[i].addEventListener('click', function(){
console.log(this.dataset.contactType);
});
}
See here your working example: https://jsfiddle.net/skmqya0u/ 在这里查看您的工作示例: https : //jsfiddle.net/skmqya0u/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.