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

jqueryweb developmentfront end technology

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

示例

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

<!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 element: " + $("div").width());
    });
});
</script>
</head>
<body>
<div style="height:200px;width:400px; margin:1px;padding:20px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Width</button>
</body>
</html>

相关文章