1

I've been trying to add a class to each list item based on their content:

$('.cl-tags').children('li').addClass( $(this).val() );

From

<ul class="cl-tags">
  <li>Green</li>
  <li>Red</li>
  <li>BLue</li>
  <li>Yellow</li>
</ul>

to

<ul class="cl-tags">
  <li class"Green">Green</li>
 ...

Apparently jQuery throws an error, is there other way of doing this?

4

1 回答 1

3

尝试这个

jQuery('.cl-tags').children('li').each(function( index ) {
   jQuery(this).addClass( jQuery(this).text() );    
});

或者

$('.cl-tags').children('li').each(function( index ) {
   $(this).addClass($(this).text());    
});

希望对你有帮助

于 2013-10-11T18:52:48.660 回答