gpt4 book ai didi

jQuery:removeClass 中的通配符类选择器

转载 作者:行者123 更新时间:2023-12-01 00:52:05 26 4
gpt4 key购买 nike

当我直接指定它时(即“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>

最佳答案

$.removeClass() 不将选择器作为参数,仅将类名(或多个类名)作为参数。

参见:Removing multiple classes (jQuery)

所以你基本上需要调用:

$.removeClass('ratingBlock1 ratingBlock2 ratingBlock3 ratingBlock4 ratingBlock5');

关于jQuery:removeClass 中的通配符类选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16039708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com