Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีตั้งค่าความกว้างสูงสุด (Max Width) ขององค์ประกอบด้วย JavaScript

ใช้คุณสมบัติ maxWidth เพื่อจำกัดความกว้างสูงสุดขององค์ประกอบ จะไม่ขยายกว้างกว่านี้แม้พื้นที่จะมาก

ตัวอย่าง

<!DOCTYPE html>
<html>
  <body>
    <p>Click below to set Maximum width.</p>
    <button type="button" onclick="display()">Max Width</button>
    <div id="box">
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
    </div>
    <br>
    <script>
      function display() {
        document.getElementById("box").style.maxWidth = "80px";
      }
    </script>
  </body>
</html>