简体   繁体   English

如何获得具有匹配数据属性值的dom元素?

[英]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.

相关问题 如何按数据属性的数值对元素进行排序? - How can I sort elements by numerical value of data attribute? 如何在dom(使用jQuery)上选择具有以“created”结尾的数据属性的元素? - How can I select elements on dom (with jQuery) that has some data attribute that ends with “created”? 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value 我怎样才能&#39;获得&#39;匹配2个属性/值对的dom元素的所有实例? - How can I 'get' all instances of a dom element that matches 2 attribute property/value pairs? Javascript DOM:在这种情况下,如何获取元素的类属性值? - Javascript DOM: How can I get the class attribute value of an element in this situation? 如何使用js / jquery将数据属性从数组获取到DOM上的h3中? - How can I get a data attribute from an array into an h3 on the DOM using js / jquery? 如何从元素中获取 jQuery 中数据属性的值? - How can I get the value of data attribute in jQuery from an element? 如何获取单击按钮的数据属性值? - How can I get data attribute value of clicked button? 如何将 data-value 属性作为字符串获取? - How can I get the data-value attribute as a string? 如何使用Selenium和Python更改DOM中的属性值 - How can I change an attribute value in the DOM using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM