gpt4 book ai didi

javascript - 如何使用JQuery在另一个div中找到div的宽度

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:08 25 4
gpt4 key购买 nike

您好,我正在尝试运行这段代码来查找另一个 div 内的 div 的宽度。但它运行不正常。如何做到这一点?

我想要 div mitem1 的宽度

代码:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#mydiv {
width: 100px;
height: 100px;
background-color: red;
}
</style>
<script language="JavaScript">
$('.menubar').each(function(menu){
var len = $(this).find('.mitem').text().length;
alert(len);
});
</script>
</head>
<body>

<div class="menubar">

<table border="1"><tr>
<td><div class="mitem1">Home</div></td>
<td><div class="mitem1">Communities<ul><li>item1</li><li>item2</li><li>item3</li><li>item4</li><li>item5</li></div></td>
<td><div class="mitem1">Project Gallery</div></td>
<td><div class="mitem1">For Our Customer</div></td>
<td><div class="mitem1">Our Services</div></td>
</tr></table>

</div>

</body>
</html>

最佳答案

$('.menubar .mitem1').each(function() {
alert($(this).width());
});

Here's a fiddle

如果你想获得宽度,包括 paddingborder(和 margin,可选),使用 outerWidth()

关于javascript - 如何使用JQuery在另一个div中找到div的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16582873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com