【问题标题】:How to use data attribute name and value in HTML-slightly如何在 HTML 中使用数据属性名称和值 - 略
【发布时间】:2021-12-21 04:20:15
【问题描述】:

我们需要获取属性值并将其设置在脚本文件中。那么,我们如何检查 date-attribute-name(id,url,etc..) 并在一个信任脚本中设置值。 请验证并告诉我您的想法。

谢谢!

【问题讨论】:

    标签: javascript html aem


    【解决方案1】:

    您可以使用 getAttribute 和 setAttribute 方法

    例如

    document.getElementById('element').getAttribute('id')
    document.getElementById('element').setAttribute('id', 'newId')
    

    【讨论】:

    • 感谢您的回复。但我需要检查属性名称并仅在 HTML 中分配它。你能帮我解决这个问题吗?
    • @SrigaDevi 你检查这个链接stackoverflow.com/a/5282801/10169261
    【解决方案2】:

    您可以使用document.getElementById('element').getAttribute('id')。当属性不存在时,你会得到一个null

    所以你可以稍后检查该属性是否存在。

    if (document.getElementById('element').getAttribute('id') !== null) {
       // continue
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 2014-05-16
      • 1970-01-01
      • 2020-09-01
      • 2010-09-23
      • 2012-05-09
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      相关资源
      最近更新 更多