【问题标题】:How to add custom attributes to standard HTML elements? [duplicate]如何为标准 HTML 元素添加自定义属性? [复制]
【发布时间】:2017-10-03 16:08:37
【问题描述】:

我试图弄清楚 Battlelog 是如何从 Edge 浏览器内部启动本机应用程序的,结果如下:

<button class="btn btn-primary" data-intro-step="4" data-bind-action="matchmake-mp-role" data-track="playnow.matchmake.cqclassic.join" data-bind-intro="next" data-expansion="0" data-game="2048" data-platform="1" data-role="1" data-experience="1">
Play now
</button>

请帮助我了解这些属性(如 data-bind-action 等)是如何添加到按钮元素的以及如何处理它们的?

【问题讨论】:

标签: javascript html


【解决方案1】:

data-* 属性是自定义属性;它们仅用于存储 Web 开发人员将在其 JavaScript 代码中使用的特定数据。

<button id="mybutton" class="btn btn-primary" data-intro-step="4" data-bind-action="matchmake-mp-role" data-track="playnow.matchmake.cqclassic.join" data-bind-intro="next" data-expansion="0" data-game="2048" data-platform="1" data-role="1" data-experience="1">
Play now
</button>

例如,要从 jquery 访问 data-intro-step 值,开发人员可以键入 $('#mybutton').attr('data-intro-step')$('#mybutton').data('data-intro-step') 并在他的代码中使用它。

通常它们是从 php 或 jquery-pluginsangularjs 等 js 框架添加的,并在 js 中使用。把你也可以从js中添加到以后使用。

【讨论】:

  • "data-* 属性不是标准" — It is a standard
  • “通常它们是从 php 添加的”——这是一个非常狭隘的世界观。
猜你喜欢
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2022-10-13
  • 2011-06-27
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
相关资源
最近更新 更多