简体   繁体   中英

Detect width using Javascript

I am looking to obtain the width of each <li> within the main navigation bar which can be obtained from the main LI. Whilst the below code works it seems to apply the same width to the following LI's rather than detecting it's own individual size.

How would I go about making this dynamic please?

$('.navbar-nav-middle li').width($('.navbar-nav-middle a').parent().width());

You need to use contextual this enclosed with a function here:

$('.navbar-nav-middle li').width(function () {
  return $(this).find("a").parent().width()
});

This might still give the same thing, as <li> takes up its content's width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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