用按钮移动段落

时间:2016-12-31 19:30:33

标签: javascript html

我有以下段落:

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

然后,我有一个带onclick功能的按钮。我希望它将段落移到中心。我怎么能这样做?

1 个答案:

答案 0 :(得分: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操作的内容。