gpt4 book ai didi

javascript - jQuery 删除或添加样式到特定类型元素的最后一个

转载 作者:行者123 更新时间:2023-12-03 00:47:18 25 4
gpt4 key购买 nike

我有一个 jQuery 页面,其中发生了一些事情。所有的功能都已经完成了。唯一不完整的是删除每个表的最后一个小时(仅最后一个小时)。我需要知道如何找到数组中特定类型元素的最后一个。表中包含的所有元素都是它们自己的数组。我需要循环遍历每个表,并且只删除每个表中的最后一个小时。我尝试了很多方法,似乎都无法实现。我下面的代码仅删除最后一个表的最后一个小时。这并没有完成我的任务。

jQuery/JavaScript 代码(除了删除最后一个 HR 之外,一切正常)

<script>
$(document).ready(function(){
var H2SingerTitles = $("h2.toggleSinger");
for (i = 2; i < H2SingerTitles.length; i++) {
var currentH2Singer = H2SingerTitles[i];
var siblingsofH2Singer =
$(currentH2Singer).nextUntil("h2.toggleSinger");
$(siblingsofH2Singer).wrapAll("<table></table>");
for (j = 0; j < siblingsofH2Singer.length; j++) {
$("hr").last().css("border-top", "none");
} // line ends for loop with j counter
} // line ends for loop with i counter
togglerHeaders.slice(0,2).removeClass("toggleSinger");
$(".toggleSinger").next().hide();
$(".toggleSinger").click(function(){
$(".toggleSinger").next().toggleClass();
});
});
</script>

我试图使用内部循环(如下)来逐个循环表,然后仅删除表中的最后一个 HR。由于某种原因它不起作用。

for (j = 0; j < siblingsofH2Singer.length; j++) {
$("hr").last().css("border-top", "none");
} // line ends for loop with j counter

下面是我的 HTML

<div id="mainPage">
<h2 class=""><span class="title">Whitney Houston</span></h2>
<div class="aboutInfo">
<img src="whitney_houston.jpg" height="150">
<a href="www.whitneyhouston.com" target="_blank"><h3><em>The Voice</em>
</h3></a>
<h3>Worldwide Famous Vocalist, Singer and Performer</h3>
<p>I will always love you</p>
<br>
<hr>
</div>

<h2 class=""><span class="title">Mariah Carey</span></h2>
<div class="aboutInfo">
<img src="mariah_carey.jpg" height="150">
<a href="www.mariahcarey.com" target="_blank"><h3><em>Whistle
Register</em></h3></a>
<h3>Worldwide Famous Vocalist, Singer and Performer</h3>
<p>Hero</p>
<br>
<hr>
</div>

<h2 class="toggleSinger"><span class="title">Michael Jackson</span></h2>
<table style="display: none;">
<div class="aboutInfo">
<img src="michael_jackson.jpg" height="150">
<a href="www.michaeljackson.com" target="_blank"><h3><em>King of
Pop</em></h3></a>
<h3>Worldwide Superstar Singer, Dancer and Performer</h3>
<p>Billie Jean</p>
<br>
<hr>
</div>
<div class="aboutInfo">
<img src="jackson_5.jpg" height="150">
<a href="www.jackson5.com" target="_blank"><h3><em>The Jackson 5</em>
</h3></a>
<h3>Worldwide Famous R and B Group</h3>
<p>ABC</p>
<br>
<hr>
</div>
</table>

<h2 class="toggleSinger"><span class="title">Diana Ross</span></h2>
<table style="display: none;">
<div class="aboutInfo">
<img src="diana_ross.jpg" height="150">
<a href="www.dianaross.com" target="_blank"><h3><em>The Original
Diva</em></h3></a>
<h3>Worldwide Superstar Singer</h3>
<p>I'm coming out</p>
<br>
<hr>
</div>
<div class="aboutInfo">
<img src="diana_ross.jpg" height="150">
<a href="www.dianaross.com" target="_blank"><h3><em>The Supremes</em>
</h3></a>
<h3>Worldwide Famous Girl Group</h3>
<p>Stop in the name of love</p>
<br>
<hr>
</div>
</table>

<h2 class="toggleSinger"><span class="title">Lionel Richie</span></h2>
<table style="display: none;">
<div class="aboutInfo">
<img src="lionel_richie.jpg" height="150">
<a href="www.lionelrichie.com" target="_blank"><h3><em></em></h3></a>
<h3>Worldwide Superstar Singer and Pianist</h3>
<p>All night long</p>
<br>
<hr>
</div>
<div class="aboutInfo">
<img src="diana_ross.jpg" height="150">
<a href="www.dianaross.com" target="_blank"><h3><em>The Commodores</em>
</h3></a>
<h3>Worldwide Famous Group and Band</h3>
<p>Brickhouse</p>
<br>
<hr>
</div>
</table>

<h2 class="toggleSinger"><span class="title">Smokey Robinson</span></h2>
<table style="display: none;">
<div class="aboutInfo">
<img src="smokey_robinson.jpg" height="150">
<a href="www.smokeyrobinson.com" target="_blank"><h3><em></em></h3></a>
<h3>Worldwide Singer and Songwriter</h3>
<p>Cruising</p>
<br>
<hr>
</div>
<div class="aboutInfo">
<img src="the_miracles.jpg" height="150">
<a href="www.themiracles.com" target="_blank"><h3><em>The Miracles</em>
</h3></a>
<h3>Worldwide Famous R and B Group</h3>
<p>Ooh Baby Baby</p>
<br>
<hr>
</div>
</table>
</div>

最佳答案

如果您尝试删除每个表上的最后一个 HR,则不需要 for 循环,请尝试此操作。

$("table").each(function(){ $(this).find("hr").last().remove(); });

关于javascript - jQuery 删除或添加样式到特定类型元素的最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53194946/

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