简体   繁体   English

使用jQuery添加类属性

[英]Adding Class Attribute using jquery

How to add class in every send paragraph using jquery. 如何使用jQuery在每个发送段落中添加类。 Please see image below. 请参见下图。 在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM