简体   繁体   English

js宽度等于父高度

[英]js width equal parent height

I have a common menu with structure nav > ul > li > a 我有一个结构nav > ul > li > a的常用菜单

If li has child dropdown menu I create dropdown toggle 如果li有子项下拉菜单,我创建下拉切换

var dropdownToggle = $('<button />', {
  'class': 'dropdown-toggle',
  'aria-expanded': false
});

container.find('.menu-item-has-children > a').after(dropdownToggle);

var parentSize = dropdownToggle.parent().height();
dropdownToggle.height(parentSize);
dropdownToggle.width(parentSize);
.dropdown-toggle {
  position: absolute;
  right: 0;
  top: 0;
}

I want the height and width .dropdown-toggle to be equal height parent (li), but it's not working 我希望高度和宽度.dropdown-toggle等于父级(li),但它不起作用

Where is a problem? 哪里有问题?

console.log(parentSize) print 42.2 - ok, this height console.log(parentSize) print 42.2 - 好吧,这个高度

I know that there is a solution with CSS padding-top: 100% , but I want to use JS 我知道有一个CSS padding-top: 100%的解决方案padding-top: 100% ,但我想使用JS

use jquery as follows 使用jquery如下

 $('.dropdown-togggle').css({ 'width':$(this).parent().width() }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

and in html add the class dropdowntoggle to whatever element you want to apply it's parent's width. 并在html中将类dropdowntoggle添加到要应用它的父元素宽度的任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM