javascript - 如何从元素中获取属性?

标签 javascript html css attributes element

我知道这 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&amp;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&amp;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/

相关文章:

php - 为什么 nav 在 Wordpress 的内部静态页面上不起作用

javascript - 从服务获取数据

javascript - 如何打破javascript中的foreach循环?

javascript - 如何在 Rails 4 中正确处理远程链接? JSON?

javascript - 将一个 div 滑过两个 div

css - 如何在 sass 中使用 map-deep-get

php - 合并 json 字符串的唯一键的值

JQuery 这个 + class.animate()

html - 为什么过渡从自动对焦开始?

javascript - 在内联样式中删除了无效的 css 属性