0

我正在研究一个对我来说很好的水平菜单,但根据设计要求,我需要更改<div id="nav-subMenu"></div>主/父菜单li没有任何ul或子菜单的高度。我写的 jquery 对我不起作用,我将不胜感激这方面的帮助。

这是一个jsFiddle的例子

if ($('#nav-wrapper ul li').has('ul'))总是回来true

示例 jQuery

jQuery(document).ready(function () {

    if ($('#nav-wrapper ul li').hasClass('active')) { //if it does have the class active
        if ($('#nav-wrapper ul li').has('ul')) {
            alert('aaaa');
            $('#nav-subMenu').css("height", "30");
        }
    }
});

示例 HTML 代码

<div id="nav-wrapper">
   <ul class="dropdown dropdown-linear" id="nav">
      <li><span class="dir"><a href="#">Home</a></span></li>
      <li><span class="dir"><a href="#">About Us</a></span></li>
      <li><span class="dir"><a href="Articles.aspx?PageID=5&amp;Language=en-us&amp;ParID=0&amp;Issue=5&amp;CID=1">Articles</a></span></li>
      <li class="active">
         <span class="dir"><a href="Page.aspx?PageID=6&amp;Language=en-us&amp;ParID=0&amp;Issue=5&amp;CID=1">Categories</a></span>
         <ul>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=18">Book Review</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=16">Business</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=3">Culture</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=2">Economy</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=19">Finance</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=17">Infrastructure</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=20">Lifestyle</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=21">Others</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=7">People</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=1">Politics</a></li>
            <li><a href="Page.aspx?PageID=6&amp;Language=1&amp;ParID=6&amp;Issue=1&amp;CID=4">Sports</a></li>
         </ul>
      </li>
      <li><span class="dir"><a href="News.aspx?PageID=3&amp;Language=en-us&amp;ParID=0&amp;Issue=5&amp;CID=1">News</a></span></li>
      <li>
         <span class="dir"><a href="Page.aspx?PageID=12&amp;Language=en-us&amp;ParID=0&amp;Issue=5&amp;CID=1">Archive</a></span>
         <ul>
            <li><a href="Page.aspx?PageID=1&amp;Language=1&amp;ParID=12&amp;Issue=1&amp;CID=1">106</a></li>
            <li><a href="Page.aspx?PageID=3&amp;Language=1&amp;ParID=12&amp;Issue=3&amp;CID=1">102</a></li>
            <li><a href="Page.aspx?PageID=4&amp;Language=1&amp;ParID=12&amp;Issue=4&amp;CID=1">103</a></li>
            <li><a href="Page.aspx?PageID=5&amp;Language=1&amp;ParID=12&amp;Issue=5&amp;CID=1">109</a></li>
         </ul>
      </li>
      <li>
         <span class="dir"><a href="Multimedia.aspx?PageID=10&amp;Language=en-us&amp;ParID=0&amp;Issue=5&amp;CID=1">Multimedia</a></span>
         <ul>
            <li><a href="Videos.aspx?PageID=11&amp;Language=1&amp;ParID=10&amp;Issue=1&amp;CID=1">Video</a></li>
         </ul>
      </li>
   </ul>
</div>
 <div id="nav-subMenu"></div>
<div id="NewsTicker"> </div>

更新:Firebug 视图以了解何时应该触发 jquery 以向 DIV 添加高度 在此处输入图像描述

4

3 回答 3

1

我发现了您的问题,并且可以按您的意愿工作,请尝试一下:

jQuery(document).ready(function () {
  if ($("#nav-wrapper li.active").length) {
    alert('yes there are .active class');
    if ($(".active").has('ul').length) {
      alert('yes .active class has ul');
      $('#nav-subMenu').css("height", "60px");
    } else {
      alert("NO .active class don't have ul");
      $('#nav-subMenu').css("height", "0px");
    }
  } else {
    alert("NO .active class found.");
  }
});

在这里的小提琴中检查一下:http: //jsfiddle.net/zMty8/27/

于 2013-01-08T06:07:09.837 回答
0

试试这个:

 if ($('#nav-wrapper ul li').has('ul')) {
     $('#nav-subMenu').css("height", "30px");
 }
 if ($('#nav-wrapper ul li').has('ul')) {
     alert('a');
     $('#nav-subMenu').css("height", "30px");
 }

参考这里

于 2013-01-08T05:48:36.727 回答
0

检查此代码:

jQuery(document).ready(function () {

    if ($('#nav-wrapper ul li').hasClass('active')) { //if it doesn't have the class active
        if ($('#nav-wrapper ul li > ul'.length > 0)) {
           $('#nav-subMenu').css("height", "30");
        }
    }
});

演示

于 2013-01-08T05:50:51.463 回答