如何找到孩子UL李一的宽度 [英] How to find the width of the child ul li a

查看:137
本文介绍了如何找到孩子UL李一的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照以下

包裹在一个div为无序列表

 < D​​IV ID =menuwrapper>
    < UL>
     <立GT;家
           < UL>
             <立GT;< A> anchor1< / A>< /李>
             <立GT;< A> anchor2< / A>< /李>
             <立GT;< A> anchor3< / A>< /李>
          < / UL>
    < /李>    <立GT;学校< /李>
    <立GT;大专< /李>
    < / UL>
    < / DIV>

我需要找到那就是anchor1,anchor2,对家庭悬停anchor3标签宽度每个锚的宽度。
我有以下的code这似乎无法工作。

  $(函数(){      $(#menuwrapper UL李)。悬停(函数(){
      变种了maxWidth = 0;
      $(本).find(UL利A)。每个(函数(){          如果(this.width>了maxWidth)
              了maxWidth = this.width;
      });      $(#menuwrapper UL李李UL A)的CSS(宽了maxWidth);
  });
 });

感谢您的帮助提前


解决方案

  $(函数(){
    变种了maxWidth = 0;
    $(#menuwrapper UL李:第一胎)悬停(函数(){。
        变量$锚= $(本).find(UL利A);
        $ anchors.each(函数(){
            如果($(本).WIDTH()>了maxWidth){
                了maxWidth = $(本).WIDTH();
            }
        });
        $ anchors.width(了maxWidth);
    });
});

I have following unordered list wrapped in a div as below

    <div id="menuwrapper">
    <ul>
     <li>home
           <ul>
             <li><a>anchor1</a></li>
             <li><a>anchor2</a></li>
             <li><a>anchor3</a></li>
          </ul>
    </li>

    <li>school</li>
    <li>college</li>
    </ul>  
    </div>

I need to find width of each anchors that is anchor1,anchor2,anchor3 tags width on hover of home. I have the following code which seems not to work

    $(function () {

      $("#menuwrapper ul li").hover(function () {
      var maxWidth = 0;
      $(this).find("ul li a").each(function () {

          if (this.width > maxWidth)
              maxWidth = this.width;
      });

      $("#menuwrapper ul li ul li a").css("width", maxWidth);
  });
 });

Thanks for the help in advance

解决方案

$(function() {
    var maxWidth = 0;
    $("#menuwrapper ul li:first-child").hover(function() {
        var $anchors = $(this).find("ul li a");
        $anchors.each(function() {    
            if ($(this).width() > maxWidth) {
                maxWidth = $(this).width();
            }
        });
        $anchors.width(maxWidth);
    });
});

这篇关于如何找到孩子UL李一的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆