jQuery根据另一个元素的高度动态改变元素的高度
我知道这个问题已经得到解答,但我仍然需要针对我的具体情况的帮助
我已经
<ul>
<li>blah blah</li>
<li>blah blah blah</li>
</ul>
设法在每个 li 之后插入一个 div 使用
$('<div class="shadow-hide"></div>') .appendTo('div#main nav.left ul li');
所以 html 现在是
<ul>
<li>blah blah</li>
<div class='shadow-hide'></div>
<li>blah blah blah</li>
<div class='shadow-hide'></div>
</ul>
但是我如何设置 div 的高度以匹配它之前的 li 的高度?
这是我到目前为止的尝试,但我想它只是得到第一个 li 的高度:
$('<div class="shadow-hide"></div>') .appendTo('div#main nav.left ul li');
var liHeight = $("div#main nav.left ul li").height();
$('div.shadow-hide').css('height', liHeight);
I know this question has been answered but I still need help for my specific situation
I have
<ul>
<li>blah blah</li>
<li>blah blah blah</li>
</ul>
I have managed to insert a div after each li using
$('<div class="shadow-hide"></div>') .appendTo('div#main nav.left ul li');
So the html is now
<ul>
<li>blah blah</li>
<div class='shadow-hide'></div>
<li>blah blah blah</li>
<div class='shadow-hide'></div>
</ul>
but how do I set the height of the div to match the height of the li before it?
here's my attempt so far but I guess it just gets the height of the first li:
$('<div class="shadow-hide"></div>') .appendTo('div#main nav.left ul li');
var liHeight = $("div#main nav.left ul li").height();
$('div.shadow-hide').css('height', liHeight);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许:
或者也许更符合您的实际用例:
编辑:如评论中所述,您不能将 div 直接嵌套在列表中,因此您可能需要重新考虑如何设计它的样式,也许替换“带有列表项的阴影隐藏”div。
perhaps:
or perhaps more in line with your actual use case:
Edit: as noted in the comments, you can't nest a div directly in a list, so you might want to rethink however you're styling this, perhaps replacing the "shadow-hide" divs with list items.
$('
') .appendTo('div#main nav.left ul li');
这行将追加每个 li 内的 div。但是您显示的标记是错误的,div 应该放在每个 li 内。现在你可以尝试下面的脚本来设置每个 li 内阴影隐藏 div 的高度。
$('<div class="shadow-hide"></div>') .appendTo('div#main nav.left ul li');
this line is going to append the div inside each of the li's. But the markup which you have shown is wrong, div should go inside each li.Now you can try the below script to set the height of shadow-hide divs within each li.
两者都可以使用
.append(function(index, html)
,我认为在这种情况下更有意义:演示。
Both can be done concisely and efficiently using
.append(function(index, html)
, which I think makes more sense in this scenario:Demo.