当我直接指定它时(即'ratingBlock'、'ratingBlock1'、'ratingBlock2'等),此代码片段可在添加新类之前删除现有类。但是当我在 removeClass ('[class^="ratingBlock"]') 中使用通配符选择器时,它不起作用。难道我做错了什么?谢谢。
<style type="text/css">
.ratingBlock {background:gold !important;}
.ratingBlock1, .ratingBlock2, .ratingBlock3, .ratingBlock4, .ratingBlock5 {background:LightGreen;}
</style>
<div class="test block ratingBlock">
Broken
<div><a href="#" class="ratingLink ratingNo-1">1+</a></div>
<div><a href="#" class="ratingLink ratingNo-2">2+</a></div>
<div><a href="#" class="ratingLink ratingNo-3">3+</a></div>
</div>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
initRatingWorks();
});
function initRatingWorks() {
jQuery("a.ratingLink").bind('click', function() {
var star = jQuery(this);
var ratingBlock = star.parents('div.test.block');
var rating = star.attr('class').match(/\d+/);
if (ratingBlock.attr('class').indexOf('ratingBlock') !== -1) {
ratingBlock.removeClass('[class^="ratingBlock"]').addClass('ratingBlock' + rating);
}
else {
ratingBlock.addClass('ratingBlock' + rating);
}
return false;
});
}
// ]]>
</script>