簡體   English   中英

除了在CSP策略中添加“ unsafe-inline”以添加內聯樣式attr之外,setAttribute()是否還有其他選擇?

[英]Is there any alternative to setAttribute() other than adding 'unsafe-inline' in CSP policy for adding inline style attr?

我試圖將內聯樣式屬性即時添加到DOM。 但是我不能在CSP策略中使用unsafe-inline。

除非將“ unsafe-inline”添加到策略中,否則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()可以替代嗎?

當然,沒有理由將內聯樣式設置為屬性。 只需使用.style

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM