![](/img/trans.png)
[英]Selecting <li> child node but not grandchildren with vanilla JavaScript
[英]Get CSS property of <ul> <li> child node with vanilla javascript
如何獲取節點的子元素的CSS屬性? 我想獲得<ul>
的<li>
元素的高度
style.scss
ul.menu {
width: 100%;
float: left;
li {
height: 28px;
}
}
JavaScript的
let elementList = this.dropdownMenu.nativeElement.children;
console.log(elementList.length);
console.log(/*height of li*/);
我更喜歡使用vanilla JavaScript的解決方案而不使用jQuery!
對於你的情況,html看起來像:
<ul class="menu">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
和css:
ul.menu {
width: 100%;
float: left;
}
ul.menu li {
height: 28px;
}
你的工作javascript應該是:
var list = document.getElementsByClassName("menu")[0];
for (var i = 0; i < list.children.length; ++i) {
var clientHeight = list.children[i].clientHeight,
offsetHeight = list.children[i].offsetHeight;
console.log(clientHeight); // with padding
console.log(offsetHeight); // with borders
}
另見這個答案 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.