[英]Adding Class Attribute using jquery
You can use .newsPostContainer p
, along with :not()
to exclude the .postDate
elements: 您可以使用
.newsPostContainer p
以及:not()
排除.postDate
元素:
$('.newsPostContainer p:not(.postDate)').addClass('posteExcerpt');
Alternatively, if you can guarantee that the target p
will always be preceded by p.postDate
, then you can select by sibling: 或者,如果可以保证目标
p
始终在p.postDate
之前,则可以通过同级来选择:
$('p.postDate + p').addClass('posteExcerpt');
Just check if the traversed 'P' tag has 'postDate' class or not, if not, this is the second 'P' tag and addClass to it. 只需检查遍历的“ P”标记是否具有“ postDate”类,否则,这是第二个“ P”标记并向其添加class。
$(".newsPostContainer").find('p').each(function(){
if(!$(this).hasClass('postDate'))
$(this).addClass('posteExcerpt');
});
Realised, you can use :not
too. 意识到,您可以使用
:not
也可以。
p.postDate + p
will select all p
that is right after a p.postDate
p.postDate + p
将选择p.postDate
之后的所有p
$(".newsPostContainer p.postDate + p").addClass('posteExcerpt')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="newsPostContainer "> <p class="postDate">postDate</p> <p>posteExcerpt</p> <p class="postDate">postDate</p> <p>posteExcerpt</p> <p class="postDate">postDate</p> <p>posteExcerpt</p> <p class="postDate">postDate</p> <p>posteExcerpt</p> <p class="postDate">postDate</p> <p>posteExcerpt</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.