gpt4 book ai didi

javascript - 如果列表 LI 具有使用 jquery 的 UL,则更改 div 的高度对我不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:24:26 25 4
gpt4 key购买 nike

我正在开发一个水平菜单,它对我来说效果很好,但根据设计要求,我需要更改 <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 View 了解何时应触发 jquery 以将高度添加到 DIV enter image description here

最佳答案

我发现了您的问题,并且它正在按您的要求工作,试试这个:

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

在此处查看 fiddle :http://jsfiddle.net/zMty8/27/

关于javascript - 如果列表 LI 具有使用 jquery 的 UL,则更改 div 的高度对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14208832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com