[英]How can I get a dom elements with a matching data attribute value?
I have a function that gets the data-name attribute value from a hovered element. 我有一个从悬停的元素获取数据名称属性值的函数。 I would like to grab the dom element on the page with the matching "data-name-match" attribute value so I can add a class to it.
我想使用匹配的“ data-name-match”属性值来获取页面上的dom元素,以便为它添加一个类。
I am most of the way there but I cannot figure out the last step that matches the element. 我已经走了很多路,但是我无法弄清楚与元素匹配的最后一步。 Is it possible to use a template literal in this situation to match the "name" variables value?
在这种情况下是否可以使用模板文字来匹配“名称”变量值?
const blocks = [...document.getElementsByClassName('footer-block_icon-block')]; console.log(blocks) blocks.forEach(block => { block.addEventListener('mouseover', () => { const name = block.dataset.name; console.log({name}) const highlight = document.querySelectorAll('[data-name-match=""]') console.log({highlight}) }); })
<div class="footer-blocks"> <div class="footer-block footer-block_header-block" data-name-match="market"> <img src="<?php echo get_bloginfo('template_directory'); ?>/img/logo_winstons-market-white.png" alt=""> <ul> <li><a href="">About</a></li> <li><a href="">Catering Menu</a></li> <li><a href="">Directions</a></li> <li><a href="">Contact</a></li> </ul> </div> <div class="footer-block footer-block_header-block" data-name-match="sausage"> <img src="<?php echo get_bloginfo('template_directory'); ?>/img/logo_winstons-sausage-white.png " alt=""> <ul> <li><a href="">About</a></li> <li><a href="">Ordering</a></li> <li><a href="">Contact</a></li> </ul> </div> <div class="footer-block footer-block_header-block" data-name-match="ashford"> <img src="<?php echo get_bloginfo('template_directory'); ?>/img/logo_ashford-house-white.png" alt=""> <ul> <li><a href="">About</a></li> <li><a href="">Dinner</a></li> <li><a href="">Lunch & Breakfast</a></li> <li><a href="">Directions</a></li> <li><a href="">Contact</a></li> </ul> </div> <div class="footer-block footer-block_icon-block" data-name="market"> <div class="footer-block_icon-block-header"> <i class="fas fa-home"></i> <a href=""> 7959 159th Street <br> Tinley Park, IL 60477 </a> </div> <div class="footer-block_icon-block-body"> <ul> <li><i class="fas fa-phone"></i>708-633-7500</li> <li><i class="fas fa-fax"></i>708-633-7552</li> <li> <i class="fas fa-clock"></i> <ul class="icon-block_sub-list"> <li>Mon - Sat: 9am - 6pm</li> <li>Sun: 10am - 3pm</li> </ul> </li> </ul> </div> </div> <div class="footer-block footer-block_icon-block" data-name="sausage"> <div class="footer-block_icon-block-header"> <i class="fas fa-home"></i> <a href=""> 4701 West 63rd Street <br> Chicago, IL 60629 </a> </div> <div class="footer-block_icon-block-body"> <ul> <li><i class="fas fa-phone"></i>773-767-4353</li> <li><i class="fas fa-fax"></i>773-767-0470</li> <li> <i class="fas fa-clock"></i> <ul class="icon-block_sub-list"> <li>Tues - Thurs: 8am - 3pm</li> <li>Sun - Mon: Closed</li> </ul> </li> </ul> </div> </div> <div class="footer-block footer-block_icon-block" data-name="ashford"> <div class="footer-block_icon-block-header"> <i class="fas fa-home"></i> <a href=""> 7959 West 159th St. <br> Tinley Park, IL 60477 </a> </div> <div class="footer-block_icon-block-body"> <ul> <li><i class="fas fa-phone"></i>708-633-7600</li> <li><i class="fas fa-fax"></i>708-633-7552</li> <li> <i class="fas fa-clock"></i> <ul class="icon-block_sub-list"> <li>Mon - Thurs: 7am - 9pm</li> <li>Fri & Sat: 7am - 10pm</li> <li>Sun: 7am - 8pm</li> </ul> </li> </ul> </div> </div> </div>
If you want ALL elements that have the attribute data-name-match
then you can do 如果您希望所有具有
data-name-match
属性的元素,则可以执行
const highlight = document.querySelectorAll('[data-name-match]');
if you want to match a specific one, it might not make sense to use querySelectorAll
as you technically just ask for one, so you could do (as you already indicated) that with a template literal, like so: 如果要匹配一个特定的
querySelectorAll
,则在技术上仅要求一个就使用querySelectorAll
可能就没有意义,因此可以(如前所述)使用模板文字来做到这一点,例如:
const highlight = document.querySelector(`[data-name-match="${name}"]`);
You need to inject the name
to the selector 您需要将
name
注入选择器
const highlight = document.querySelectorAll('[data-name-match="'+name+'"]')
or you could use template literals 或者您可以使用模板文字
const highlight = document.querySelectorAll(`[data-name-match="${name}"]`)
Is it possible to use a template literal in this situation to match the "name" variables value?
在这种情况下是否可以使用模板文字来匹配“名称”变量值?
Should be, yes. 应该是的。
const highlight = document.querySelectorAll(`[data-name-match="${name}"]`)
If there should only be one of those, you can use document.querySelector
which will only match the first one and you won't need to iterate over highlight
. 如果应该只有其中的一个,你可以使用
document.querySelector
将只匹配第一个,你会不会需要遍历highlight
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.