djmhim
djmhim

Reputation: 59

How can I apply CSS

I have a list of tags like tag 01, tag 02, tag 03, tag 04, tag 05, and so on...

I want to apply CSS, which puts a class for tag 01, and I assign a color, etc., to it. Same for tag 02, tag 03 and so on.

The problem is, I can't edit the HTML where it is like:

<p>tag 01, tag 02, tag 03, tag 04, tag 05</p>

I don't know if it is possible to put a class using Java, CSS, or anything else for each tag.

Please let me know if it is possible, and if yes then how it is possible.

Upvotes: 1

Views: 71

Answers (1)

Farhad Bagherlo
Farhad Bagherlo

Reputation: 6699

$("p.tag").each(function(){
  var $this=$(this);
  var Tag=$this.text().split(',');
  $this.empty()
  $.each(Tag,function(key,value){
    $this.append(key==0?"":"<span>,</span>").append( $("<span/>",{class:("tag"+ (key+1)),html:value}));
  });
});
.tag1{color:red};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="tag">tag 01, tag 02, tag 03, tag 04, tag 05</p>
<p class="tag">tag 01, tag 02, tag 03, tag 04, tag 05</p>
<p class="tag">tag 01, tag 02, tag 03, tag 04, tag 05</p>

Upvotes: 2

Related Questions