2016-03-06 32 views
0

這是我的HTML無法設置CSS規則,JS

<div id="myButton" class="selfDistrucitonButton"> 
    <div class="outer"> 
    <div class="height"> 
     <div class="inner"> Button </div> 
    </div> 
    </div> 
</div> 

這裏是JS

document.getElementsByName('myButton').style.marginLeft = "90px"; 
document.getElementsByName('myButton').style.marginTop = "40px"; 
document.getElementsByName('myButton').style.width = "400px"; 

它產生在控制檯上的錯誤:「無法設置的未定義的屬性」。

即使console.log("document.getElementById("myButton");它對我說,按鈕是NULL。我真的不明白問題在哪裏。

回答

0

你必須使用document.getElementByIdname

document.getElementById('myButton').style.marginLeft = "90px"; 

由於您的div由具有id屬性與價值myButton。不是名字。

而且完整的代碼會是這樣,

var elem = document.getElementById('myButton'); 
elem.style.marginLeft = "90px"; 
elem.style.marginTop = "40px"; 
elem.style.width = "400px"; 
+0

已經試過了,沒有工作,同樣的錯誤。 – Giulio

+0

@Giulio你把你的腳本標籤放在哪裏? –

+0

它在頭 – Giulio