我知道这 4 个属性 getElementById、getElementsByClassName、getElementsByTagName 和 getElementsByName 但是 Facebook 有一个有趣的属性叫做 ajaxify
<a role="button" class="_42ft _4jy0 _4jy3 _517h _51sy" href="#" ajaxify="/ajax/groups/mall/approve/?group_id=29702291481682&message_ids=490372844781280" rel="async-post">
是的...,我只想学习如何获取自定义属性
最佳答案
如果您想根据属性获取特定元素,您可以使用 querySelector()
或 querySelectorAll()
使用 Attribute Selector .
const el = document.querySelector('[ajaxify]');
console.log(el.getAttribute('ajaxify'));
<div>
<a role="button" class="_42ft _4jy0 _4jy3 _517h _51sy" href="#" ajaxify="/ajax/groups/mall/approve/?group_id=29702291481682&message_ids=490372844781280" rel="async-post">dasdas</a> -
<a href="somewhere.com">somewhere.com</a>
</div>
第一行查找具有名为 'ajaxify'
的属性的第一个元素。
第二行获取该元素上的 'ajaxify'
属性的值。
关于javascript - 如何从元素中获取属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857000/