onclick函数javascript

时间:2019-05-17 18:46:33

标签: javascript html css

我有一个textarea和3个按钮,我需要将文本与这3个按钮对齐:左,中,右

<button type="button" onclick="myFunction()">LEFT</button>
<script>
  function myFunction() {
    document.getElementById("myP").style.textAlign = "left";
  }
</script>
<button type="button" onclick="myFunction()">CENTER</button>
<script>
  function myFunction() {
    document.getElementById("myP").style.textAlign = "center";
  }
</script>
<button type="button" onclick="myFunction()">RIGHT</button>
<script>
  function myFunction() {
    document.getElementById("myP").style.textAlign = "right";
  }
</script>
<textarea name="textarea" id="myP" rows="10" cols="40"></textarea>
</div>

1 个答案:

答案 0 :(得分:1)

  function myFunction(align) {
    document.getElementById("myP").style.textAlign = align;
  }
<button type="button" onclick="myFunction('left')">LEFT</button>
<button type="button" onclick="myFunction('center')">CENTER</button>
<button type="button" onclick="myFunction('right')">RIGHT</button>
</script>
<textarea name="textarea" id="myP" rows="10" cols="40"></textarea>
</div>