简体   繁体   English

检查元素属性是否具有特定值JS

[英]Check if element attribute has specific value JS

Is there a way in javascript to check if an element has a certain attribute value? javascript中有没有办法检查元素是否具有某个属性值? Given this code:鉴于此代码:

<div class="wrapper">
   <a href=#refresh-cart>view cart</a>
</div>

is there a way to be more specific rather than有没有办法更具体而不是

document.addEventListener('click', e => {
  if (e.target.hasAttribute('href')) {
    //...
  }
})

Get the value.获取价值。 Compare it to what you want.将其与您想要的进行比较。

if (e.target.getAttribute('href') === "foo")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM