我有一个有很多评论的页面。
代码结构如下:
+ <li id="li-comment-65" class="comment even thread-odd thread-alt depth-1">
<div class="comment-body">
<div id="comment-65">
<div class="comment-author vcard">
<img height="30" width="30" href="" alt="" />
<cite class="fn">keith</cite>
</div>
<div class="comment-meta commentmetadata">
<a href="#">June 16, 2009 at 10:21 pm</a>
</div>
<div id="rating-65" class="rating-class">Excellent</div>
<p>Hiya</p>
</div>
</div>
</li>我想做的是:
这很难做到吗?
发布于 2009-06-17 00:24:35
这应该可以做到:
$('div.rating-class').each(function() {
var value = $.trim($(this).text());
var src;
switch(value) {
case 'Excellent':
src = 'fivestars.png';
break;
case 'Very Good':
src = 'fourstars.png';
break;
...
}
$img = $('<img/>').attr('src', src);
$(this).html($img);
});更好的做法是这样做:
$('div.rating-class').each(function() {
var value = $.trim($(this).text()).replace(' ', '_').toLowerCase();
$(this).addClass(value);
});然后有这样的CSS类:
div.rating-class.excellent {
background-image: url(fivestars.png);
text-indent: -1000px;
}
div.rating-class.very_good {
background-image: url(fourstars.png);
text-indent: -1000px;
}
...其中的文本缩进将隐藏您原来有的常规文本。
https://stackoverflow.com/questions/1004534
复制相似问题