Javascript 将类添加到文章的最后一节
我想要的是在文章的最后一节添加一个带有“class2”的类。如果你能帮我解决这个问题,那就太好了。Javascript 将类添加到文章的最后一节,javascript,jquery,html,Javascript,Jquery,Html,我想要的是在文章的最后一节添加一个带有“class2”的类。如果你能帮我解决这个问题,那就太好了。 谢谢你的时间和回答。对不起,我的英语不好 <article class="class1"> <section> <p>...</p> </section> <section> <p>something</p> </section>
谢谢你的时间和回答。对不起,我的英语不好
<article class="class1">
<section>
<p>...</p>
</section>
<section>
<p>something</p>
</section>
</article>
<article class="class2">
<section>
<p>lorem ipsum</p>
</section>
<section>
<p>add class to this section</p>
</section>
</article>
某物
同侧眼睑
将类添加到此节
试试这个
$('.class2 section:last').addClass("test");
使用:last
$('.class2 section:last').addClass("custom-class");
仅使用Js
var sections = document.querySelectorAll("article.class2 section"); // This way you get an array with all the <section> in your article.class2
sections[sections.length-1].className="class"; //with the first part you take the last element of the array and then you apply the class you want
var sections=document.querySelectorAll(“article.class2 section”);//这样您就得到了一个包含文章中所有内容的数组。class2
节[sections.length-1].className=“class”//在第一部分中,获取数组的最后一个元素,然后应用所需的类
$('.class2节:last').addClass(“class”)代码>