gpt4 book ai didi

document.createElement 中的 Javascript .style 不起作用

转载 作者:行者123 更新时间:2023-11-28 19:04:09 26 4
gpt4 key购买 nike

这是我的代码:

        var link = document.createElement("a");
link.id = "control_random";
link.href = document.location.href + "#";
//link.style = "margin-left:200px";

// 标记的代码不起作用。当我使用检查元素编辑它时,我注意到它尚未应用。

最佳答案

您必须使用style.cssText

var link = document.createElement("a");
link.id = "control_random";
link.style.cssText = "margin-left:200px; margin-right:100px;";

其他方式...

link.setAttribute("style", "margin-left:200px; margin-right:100px;");

(感谢@T.J.克劳德)

link.style.marginLeft = "200px";
link.style.marginRight = "100px";

关于document.createElement 中的 Javascript .style 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com