2016-12-31 50 views
-5

我有以下段落:把第一個按鈕

<p id="paragraph"> 
    <u> 
    My First Paragraph 
    </u> 
</p> 

然後,我有onclick功能的按鈕。我希望它將該段移到中心位置。我怎樣才能做到這一點?

+0

你能否詳細說明你的意思是「中心」?保證金明智?垂直中心?在文字前面? – Stardust

+0

我的意思是水平居中 –

+0

呵呵,這樣就像文字對齊?那麼你會使用文本對齊:中心 – Stardust

回答

1

function centerThis() { 
 
    document.getElementById("paragraph").style.textAlign = "center"; 
 
}
#paragraph { 
 
    text-align:left; 
 
    width:auto; 
 
}
<p id="paragraph"> 
 
    <u> 
 
    My First Paragraph 
 
    </u> 
 
</p> 
 

 
<button onclick="centerThis()">Click me</button>

正如你可以看到,當點擊該按鈕時,該方法centerThis()被調用,並且該段的中心。有關更多信息,請參見http://www.w3schools.com/jsref/prop_style_textalign.asp。我會建議閱讀關於DOM操作。

+0

謝謝!我會將它標記爲我的答案! –

+0

Np!我很高興能夠提供幫助。新年快樂! – Stardust

相關問題