jQuery 删除或添加样式到特定类型元素的最后一个
jQuery remove or add style to last of specific type of element
我有一个 jQuery 页面,其中有一些事情正在发生。所有的功能都是完整的。唯一不完整的是删除每个 table 的最后一小时(仅最后一小时)。我需要知道如何处理数组中特定类型元素的最后一个。 table 中包含的所有元素都是它们自己的数组。我需要遍历每个 table,并且只删除每个 table 的 table 中的最后一个小时。我尝试了很多方法,但似乎无法得到它。我下面的代码仅删除最后 table 的最后一小时。这没有完成我的任务。
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>
内部循环(在下面)是我试图用来一个接一个循环遍历 table 然后仅删除 table 中的最后一个 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>
如果你想删除每个 table 上的最后一个 HR,你不需要 for 循环,试试这个。
$("table").each(function(){ $(this).find("hr").last().remove(); });
我有一个 jQuery 页面,其中有一些事情正在发生。所有的功能都是完整的。唯一不完整的是删除每个 table 的最后一小时(仅最后一小时)。我需要知道如何处理数组中特定类型元素的最后一个。 table 中包含的所有元素都是它们自己的数组。我需要遍历每个 table,并且只删除每个 table 的 table 中的最后一个小时。我尝试了很多方法,但似乎无法得到它。我下面的代码仅删除最后 table 的最后一小时。这没有完成我的任务。
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>
内部循环(在下面)是我试图用来一个接一个循环遍历 table 然后仅删除 table 中的最后一个 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>
如果你想删除每个 table 上的最后一个 HR,你不需要 for 循环,试试这个。
$("table").each(function(){ $(this).find("hr").last().remove(); });