把第一个按钮

问题描述:

我有以下段落:把第一个按钮

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

然后,我有onclick功能的按钮。我希望它将该段移到中心位置。我怎样才能做到这一点?

+0

你能否详细说明你的意思是“中心”?保证金明智?垂直中心?在文字前面? – Stardust

+0

我的意思是水平居中 –

+0

呵呵,这样就像文字对齐?那么你会使用文本对齐:中心 – Stardust

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