gpt4 book ai didi

javascript - 第 N 个子 css 属性正在计算 display 属性设置为 none 的 html 元素。怎么改?

转载 作者:可可西里 更新时间:2023-11-01 13:13:45 24 4
gpt4 key购买 nike

我有一个使用无序列表的简单画廊。

    <h1>Projects</h1>
<hr>

<!-- Projects gallery as unordered list -->

<ul class="gallery">

<li class="item residential">
<img src="Projects/01-HighTorEast-EarlShilton/01-thumbnail.jpg" width="212" height="119" alt="High Tor East, Earl Shilton">
<h2><a class="info" href="Projects/01-HighTorEast-EarlShilton/info.php">High Tor East, Earl Shilton</a></h2>
<h3><a class="cat" href="residential">Residential</a></h3>
</li>

<li class="item modernisation">
<img src="Projects/02-Hollycroft-Hinckley/02-thumbnail.jpg" width="212" height="119" alt="Hollycroft, Hinckley">
<h2><a class="info" href="Projects/02-Hollycroft-Hinckley/info.php">Hollycroft, Hinckley</a></h2>
<h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3>
</li>

<li class="item residential">
<img src="Projects/03-SpaLane-Hinckley/03-thumbnail.jpg" width="212" height="119" alt="Spa Lane, Hinckley">
<h2><a class="info" href="Projects/03-SpaLane-Hinckley/info.php">Spa Lane, Hinckley</a></h2>
<h3><a class="cat" href="residential">Residential</a></h3>
</li>

<li class="item residential">
<img src="Projects/04-Farnhambridge-Rothley/04-thumbnail.jpg" width="212" height="119" alt="Farnhambridge Farm, Rothley">
<h2><a class="info" href="Projects/04-Farnhambridge-Rothley/info.php">Farnhambridge Farm, Rothley</a></h2>
<h3><a class="cat" href="residential">Residential</a></h3>
</li>

<li class="item modernisation">
<img src="Projects/05-NetherfieldClose-BroughtanAstley/05-thumbnail.jpg" width="212" height="119" alt="Netherfield Close, Broughtan Astley">
<h2><a class="info" href="Projects/05-NetherfieldClose-BroughtanAstley/info.php">Netherfield Close, Broughtan Astley</a></h2>
<h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3>
</li>

<li class="item modernisation">
<img src="Projects/06-Bridlepath-Elmesthorpe/06-thumbnail.jpg" width="212" height="119" alt="Bridlepath, Elmesthorpe">
<h2><a class="info" href="Projects/06-Bridlepath-Elmesthorpe/info.php">Bridlepath, Elmesthorpe</a></h2>
<h3><a class="cat" href="modernisation">Modernisation & Domestic Extensions</a></h3>
</li>

<li class="item residential">
<img src="Projects/07-Bridlepath-Elmesthorpe/07-thumbnail.jpg" width="212" height="119" alt="Bridlepath, Elmesthorpe">
<h2><a class="info" href="Projects/07-Bridlepath-Elmesthorpe/info.php">Bridlepath, Elmesthorpe</a></h2>
<h3><a class="cat" href="residential">Residential</a></h3>
</li>

<li class="item feasibility">
<img src="Projects/08-GrangeCroft-Ullesthorpe/08-thumbnail.jpg" width="212" height="119" alt="Grange Croft, Ullesthorpe">
<h2><a class="info" href="Projects/08-GrangeCroft-Ullesthorpe/info.php">Grange Croft, Ullesthorpe</a></h2>
<h3><a class="cat" href="feasibility">Feasibility layouts</a></h3>
</li>

<li class="item master">
<img src="Projects/09-RailwayInn-Sileby/09-thumbnail.jpg" width="212" height="119" alt="The Railway Inn, Sileby">
<h2><a class="info" href="Projects/09-RailwayInn-Sileby/info.php">The Railway Inn, Sileby</a></h2>
<h3><a class="cat" href="master">Master planning</a></h3>
</li>


</ul>

</section>

在我的 css 中,我将 .item 类的每四个 child 的右边距值设置为零。我这样做是为了在我的页面上将四个元素排成一行,并且在不删除边距的情况下我只能容纳三个。

ul.gallery { clear:both; list-style:none; margin:0; padding:0; width:940px; }
.gallery a:hover { text-decoration:underline; }
li.item { display:inline; margin:30px 20px 30px 0px; padding:0px; height:178px; width:220px; float:left; background:#ededed url('../Images/featuredline.png') repeat-x 0% 100%; overflow:hidden; }
li.item:nth-child(4n+4) { margin:30px 0px 30px 0px; }

接下来,我编写了一些小的 jQuery 代码来按类别对元素进行排序。因此,如果有人点击“住宅”链接,它将隐藏不同类别的元素。

    $('.cat').bind('click', function(e) {
var cat = $(this).attr('href');
$('.item').each(function () {
var itemClass = $(this).attr('class');
if (itemClass != 'item '+cat) {
$(this).css({"display":"none"});
};


});
e.preventDefault();

我的问题是:当我使用上面的 jQuery 脚本对我的画廊元素进行排序时,似乎 .item:nth-child 属性仍在计算显示属性设置为无的元素。我不知道怎么咬它。我需要 css .item:nth-child 属性在 jQuery 插件启动后只计算可见元素。

网站在这里可用 http://www.damianwojtczak.com/avd2/projects.php

最佳答案

删除你的第 nth-child CSS 样式,每次你改变布局时,调用这个:

$("li.item:visible").each(function(i) {
if((i+1)%4==0) $(this).css("margin","30px 0");
else $(this).css("margin","30px 20px 30px 0");
})

刚刚在您的站点上使用 firebug 对其进行了测试,效果很好。

关于javascript - 第 N 个子 css 属性正在计算 display 属性设置为 none 的 html 元素。怎么改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664050/

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