简体   繁体   中英

Changing height of div if list LI has UL using jquery not working for me

I am working on a Horizontal menu which works fine for me but as per design requirement i need to change the height of <div id="nav-subMenu"></div> if main/parent menu li doesn't have any ul or submenus in other words. jquery which i wrote is not working for me i would appreciate help in this regard.

Here is an example of jsFiddle

if ($('#nav-wrapper ul li').has('ul')) always return true

Sample 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");
        }
    }
});

Sample HTML code

<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>

UPDATE: Firebug view to get idea when jquery should fire to add height to DIV 在此处输入图片说明

I found your problem and this is working as you desired, try this out:

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.");
  }
});

check this out in the fiddle here: http://jsfiddle.net/zMty8/27/

Try this:

 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");
 }

Reference here

Check this code:

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");
        }
    }
});

DEMO

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