我有一个包含大量评论的页面。
代码结构如下:
+ <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>
我想做的是以下内容:
这很难吗?
答案 0 :(得分:3)
这应该这样做:
$('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;
}
...
文本缩进会隐藏您原来在那里的常规文本。