如何用 JavaScript 设置元素的最小宽度?
在 JavaScript 中使用 minWidth 属性来设置最小宽度。你可以尝试运行以下代码来用 JavaScript 设置元素的最小宽度 -
示例
<!DOCTYPE html> <html> <head> <style> #box { width: 50%; background-color: gray; } </style> </head> <body> <p>Click below to set Minimum width.</p> <button type="button" onclick="display()">Min Width</button> <div id="box"> <p>This is a div. This is a div. 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. This is a div. This is a div.</p> <p>This is a div. This is a div. 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. This is a div. This is a div.</p> <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p> </div> <br> <script> function display() { document.getElementById("box").style.minWidth = "500px"; } </script> </body> </html>
广告