如何使用 jQuery 获取 div 元素的宽度?

jqueryweb developmentfront end technology

要获取 div 元素的宽度,请使用 jQuery 中的 width() 方法。

示例

您可以尝试运行以下代码,使用 jQuery 获取 div 元素的宽度:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("Width of div element: " + $("div").width());
    });
});
</script>
</head>
<body>
<div style="height:200px;width:450px;padding:20px;margin:1px;border:1px solid red;background-color:gray;"></div><br>
<button>Get Width of div</button>
</body>
</html>

相关文章