onclick函数javascript

onclick函数javascript

我有一个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>

最佳答案

  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>

关于javascript - onclick函数javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56192010/

10-13 01:55