[英]How do I get the value of the first child element in Javascript?
I'm trying to get the value of the first child element from my HTML to show whenever I click on an image aka my 'services-cell" container, but it keeps saying the value is undefined.我试图从我的 HTML 中获取第一个子元素的值,以便在我单击图像(即我的“服务单元”容器)时显示,但它一直说该值未定义。
<div class="services-cell">
<img class="services-cell_img" src="gallery/img-1.jpg" alt="">
<div class="services-cell_text">Digital Marketing</div>
</div>
Here is my Javascript这是我的 Javascript
let galleryImages = document.querySelectorAll('.services-cell')
if(galleryImages) {
galleryImages.forEach(function(image){
image.onclick = function() {
console.log(galleryImages.firstElementChild.value)
}
})
}
I tried to add the img class as a variable as well, but it also says undefined.我尝试将 img class 也添加为变量,但它也显示未定义。 I want the console.log to print
我希望 console.log 打印
<img class="services-cell_img" src="gallery/img-1.jpg" alt="">
I have multiple images with the same html except it just say img-2, img-3 etc. So ideally whenever I click on the other images it would print the same HTML value but just will say the image number that I clicked on我有多个具有相同 html 的图像,除了它只是说 img-2、img-3 等。所以理想情况下,每当我点击其他图像时,它都会打印相同的 HTML 值,但只会说出我点击的图像编号
You created the array as galleryImages
, but then rather than accessing the firstElementChild
of the div
, you're trying to access that property on the array.您将数组创建为
galleryImages
,但随后不是访问div
的firstElementChild
,而是尝试访问数组上的该属性。 You need to do image.firstElementChild
instead.您需要改为执行
image.firstElementChild
。 Also, as far as I know, accessing .value
of an image has no meaning, I think you meant to just do firstElementChild
instead:另外,据我所知,访问图像的
.value
没有任何意义,我认为你的意思是只做firstElementChild
:
let galleryImages = document.querySelectorAll('.services-cell'); if (galleryImages) { galleryImages.forEach(function (image) { image.onclick = function() { console.log(image.firstElementChild); }; }); }
<div class="services-cell"> <img class="services-cell_img" src="https://s3.amazonaws.com/pix.iemoji.com/images/emoji/apple/ios-12/256/deciduous-tree.png" alt=""> <div class="services-cell_text">Digital Marketing</div> </div> <div class="services-cell"> <img class="services-cell_img" src="https://cdn-cloudfront.cfauthx.com/binaries/content/gallery/gg-en-us/icons/gg-tree-icon.png" alt=""> <div class="services-cell_text">Digital Marketing</div> </div>
What you could do to achieve this is passing the event attribute as a parameter of the onclick function.你可以做的是传递事件属性作为 onclick function 的参数。 The event attribute has a target;
事件属性有一个目标; which is the item that triggered the event.
这是触发事件的项目。 So for example:
例如:
if(galleryImages) {
galleryImages.forEach(function(image){
image.onclick = function(e) {
console.log(e.target.firstElementChild.value)
}
})
}
However instead of adding an eventListener to every element, it might be better to add one event handler on the parent - and check which item is clicked.然而,与其为每个元素添加一个事件监听器,不如在父元素上添加一个事件处理程序 - 并检查单击了哪个项目。 Eg
例如
<div class="parent">
<div class="services-cell">
<img class="services-cell_img" src="gallery/img-1.jpg" alt="" />
<div class="services-cell_text">Digital Marketing</div>
</div>
<div class="services-cell">
<img class="services-cell_img" src="gallery/img-2.jpg" alt="" />
<div class="services-cell_text">Digital Marketing</div>
</div>
</div>
And the javascript:和 javascript:
document.querySelector('.parent').addEventListener('click', function(e){
if(e.target.matches('.services-cell'){
// Do something with the e.target - which is the .services.cell
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.