通过特定的数据属性获取元素



有没有办法通过javascript或jQuery通过元素的一个数据属性来获取整个元素?假设我在像这样的网站上有一个链接

<a href="http://www.google.com/" data-source="google">Google</a>

由于上面的链接没有任何id或类,我无法通过它来获取它。我需要通过它的data-source属性来获取它。假设我可以用jQuery获得一个名为data-source的属性的值。我该怎么做?

$('[data-source="google"]')

这将选择data-source属性等于google的所有元素。

请参见属性等于选择器[name="value"]。

最新更新