您好我正在使用JavaScript在新選項卡中創建動態表單。對div的保證金效應不起作用。我既不能在CSS中使用class和id設置邊距,也不能使用div.setAttribute。以下是代碼。 `在新選項卡div邊緣不起作用
var openWindow = window.open('dynForm.html','_blank','');
var windoc=openWindow.document;
windoc.write("This is dynamic form ");
var div=document.createElement("div");
div.setAttribute("style","border:solid");
div.setAttribute("style","margin-left:1cm");
div.setAttribute("style","margin-right:1cm");
div.setAttribute("style","margin-bottom:1cm");
div.setAttribute("style","margin-top:1cm");
div.setAttribute("style","background-color:lightblue");
`只有背景顏色,走的是新的選項卡上的效果。下面的屏幕截圖顯示了屏幕的外觀如何與邊距值無關。
Quesstion 2:在我的CSS沒有class和id屬性被應用到DIV。
#myDIV2{
background-color:lightblue;
}
我試圖div.id="myDIV2";
我也試過div.setAttribute("id","myDIV2")
;`我申請使用類也一樣,但還是沒找到任何區別。我不知道爲什麼它不工作,這裏出了什麼問題。請幫助
我想象是因爲每次調用'div.setAttribute'時你都覆蓋整個'style'屬性。所以只應用最後的效果。您可能需要一種方法來複合它們。 – Gorbles
我應該如何修改我的代碼以獲得我想要的。你也可以告訴我爲什麼#myDIV2沒有對新標籤中的div生效。我在添加#myDIV2之前評論所有setAttribute行。這是我的第二個問題。我想知道的是如何從css文件中添加一個類到新選項卡中的div元素。 –