Javascript 除了添加';不安全内联';是否在CSP策略中添加内联样式属性?

Javascript 除了添加';不安全内联';是否在CSP策略中添加内联样式属性?,javascript,css,content-security-policy,inline-styles,Javascript,Css,Content Security Policy,Inline Styles,我正在尝试向DOM动态添加内联样式属性。但我不能在CSP策略中使用不安全内联 CSP不允许添加内联样式属性,除非将“不安全内联”添加到策略中 var div1 = document.createElement('div'), body = document.getElementsByTagName('body'); div1.setAttribute("style", "position:absolute;top:0;left:0;margin:0;"); body[0].appendChild

我正在尝试向DOM动态添加内联样式属性。但我不能在CSP策略中使用不安全内联

CSP不允许添加内联样式属性,除非将“不安全内联”添加到策略中

var div1 = document.createElement('div'), body = document.getElementsByTagName('body');
div1.setAttribute("style", "position:absolute;top:0;left:0;margin:0;");
body[0].appendChild(div2);
是否有setAttribute()的替代方案

当然,没有理由将内联样式设置为属性。就用吧

是否有setAttribute()的替代方案

当然,没有理由将内联样式设置为属性。就用吧

var div = document.body.appendChild(document.createElement('div'));
div.style.position = "absolute";
div.style.top = 0;
div.style.left = 0;
div.style.margin = 0;