简体   繁体   English

js怎么添加中间有空格的属性?

[英]How can you add an attribute that has a space in between with js?

Ok, so here's part of a code.好的,这是代码的一部分。 I got it from a website.我是从一个网站上得到的。 I was given the code so I can embed it on another website.我得到了代码,所以我可以将它嵌入另一个网站。 It's a live chat btw.顺便说一句,这是一个实时聊天。

I need to inject the JS embed code through JS.我需要通过 JS 注入 JS 嵌入代码。 Here's part of the code.这是代码的一部分。 (I deleted some unnecessariy info) (我删除了一些不必要的信息)

<script id="abcd" data-cfasync="false" async src="//ab.abc.com/js/gz/emb.js" style="width: 272px;height: 440px;">{"handle":"nitrotype","arch":"js","styles":{"a":"0084ef","b":100,"c":"FFFFFF","cvh":30}}</script>

I tried to do it using attributes.我尝试使用属性来做到这一点。

var x = document.createElement("script");
    x.setAttribute("id", "abcd"
    x.setAttribute("data-cfasync = "false";
    x.setAttribute("async src

Well.. right there.嗯..就在那里。 async src it doesn't work. async src它不起作用。 JS doesn't recognize that. JS不承认这一点。 So I'm wondering if there is a work around this.所以我想知道是否有解决方法。

If you guys have any ideas, please let me know.如果你们有任何想法,请告诉我。 thx.谢谢。

And, by the way, the > after 440px is part of it.而且,顺便说一下,440px 之后的>是其中的一部分。 That's the other thing.那是另一回事。

While it may look like the attribute is called async src due to async not having an = after it, they are actually two separate values.虽然由于async后面没有= ,因此该属性看起来可能被称为async src ,但它们实际上是两个独立的值。 As perthe HTML spec , an attribute without a value defaults to the empty string.根据HTML 规范,没有值的属性默认为空字符串。 This is essentially for boolean attributes (either it's there or it's not), such as async and defer for script tags, or disabled for input tags.这本质上是针对 boolean 属性(存在或不存在),例如脚本标签的asyncdefer ,或输入标签的disabled So, you'd just set two attributes:所以,你只需设置两个属性:

x.setAttribute("async", "");
x.setAttribute("src", "//ab.abc.com/js/gz/emb.js");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM