javascript - 返回 data-football 属性名
问题描述
如果我有以下代码:
<button id="btnOne" data-football="Mitre">Press Me</button>
我将如何获得数据标签的名称,所以在这种情况下它将是“数据足球”。我知道如何获得值,但想知道如何获得数据标签的名称——在这种情况下,按下按钮时的数据足球。
IE,
$(document).on("click", "button", function(){
// Get the name of the attribute (data-football - not the value);
});
谢谢。
解决方案
您可以使用attributes
属性来获取 HTML 标记的所有属性
<div id="_id" data-football='test'></div>
<script>
const attrs = document.getElementById('_id').attributes
const keyName = attrs[1].nodeName // will be data-football
</script>
推荐阅读
- css - 手动更改 Bootstrap.css 中的容器宽度是否足够?
- kubernetes - 如何修复 kubernetes nfs 挂载错误没有这样的文件或目录
- python - 我们是否需要 GPU 系统来训练深度学习模型?
- intellij-idea - IntelliJ Idea + Dart 类 - 如何查看特定类的文档注释
- html - 没有输入标签的 Facebook 评论框如何工作?
- php - 如何从另一个字段获取/输入 id?
- vba - “Microsoft Internet Controls”参考有时需要重新分配
- c - 当我想通过 Winsock 下载文件时,我收到了来自服务器的错误请求
- wix - 如何修复我的 msi VerifyReadyDlg 安装按钮
- php - 转换时间戳时的土耳其语编码问题