【问题标题】:How to add HTML tags with attribute containing hyphen in reactJs?如何在 reactJs 中添加带有包含连字符的属性的 HTML 标签?
【发布时间】:2019-08-13 18:15:21
【问题描述】:

我想在我的 react 应用中添加脚本标签。 我不知道如何处理包含连字符的属性。

以下代码在'ComponentDidMount()'中

const script2 = document.createElement("script");
script2.src ="https://www.dropbox.com/static/api/2/dropins.js";
script2.type = "text/javascript";
script2.id = "dropboxjs"
script2.data-app-key = "app key" // Parsing error: Invalid left-hand side in assignment expression
script2["data-app-key"] = "app key" // Also doesn't work

document.body.appendChild(script2)

【问题讨论】:

    标签: javascript reactjs dropbox dropbox-api


    【解决方案1】:

    你应该使用dataset,自定义属性的名称应该在camelCase

    script2.dataset.appKey = 'app key'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 2016-09-28
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多