在[href * =“#”]中使用自定义属性

时间:2017-04-11 19:00:21

标签: jquery

这是我的HTML

<a data-new-count="/new/button" href="mysite.com/folder/page/new.html">

有人可以帮我弄清楚如何在data-new-count="/new/button"脚本中加入a[href*="#"]自定义属性?

当我在HTML中没有自定义属性时,

a[href*="#"]可以正常工作。

1 个答案:

答案 0 :(得分:0)

a[href*="#"]不是脚本,而是attribute selector。这样的记录意味着我们在href中获得包含a的所有锚点#。您可以在此示例中看到它:

[].map.call(document.getElementsByTagName('a'), function(element) {
	alert(element.getAttribute('data-new-count'));
});
a[href*="#"] {
  color: green;
}
<a data-new-count="/new/button" href="mysite.com/folder/page/new.html">link1</a>
<a data-new-count="/new/button2" href="mysite.com/#/folder/page/new.html">link2</a>

但是,如果您想从自定义data-属性中获取值,则可以使用代码段中的代码。

相关问题