如何使用 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 日

266 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告