如何使用 JavaScript 設定元素的水平對齊?


要水平對齊元素,使用 cssFloat 屬性。

示例

你可以嘗試執行以下程式碼,以用 JavaScript 來設定元素的水平對齊:−

線上演示

<!DOCTYPE html>
<html>
   <body>
      <img id="myID" src="https://tutorialspoint.tw/videotutorials/images/tutor_connect_home.jpg" width="300" height="300">
      <p>This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
      </p>
      <button type="button" onclick="display()">Align Horizontally</button>
      <script>
         function display() {
            document.getElementById("myID").style.cssFloat = "right";
         }
      </script>
   </body>
</html>

更新於:2020 年 6 月 23 日

267 次瀏覽

啟動你的職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.