gpt4 book ai didi

javascript - removeClass() 不会删除类

转载 作者:行者123 更新时间:2023-12-02 19:44:16 29 4
gpt4 key购买 nike

removeClass 不会从下面的 js 中的 div.upvote 中删除类 selected,但 addClass() 可以正常工作。这是为什么?

$(document).ready(function () {
"use strict";

$('div.upvote').click(function () {
var id = $(this).parents('li.book').attr('id');
var vote_type = 'up';

if ($(this).hasClass('selected')) {
var vote_action = 'recall-vote';
$.post('/b/vote/', {id: id, type: vote_type, action: vote_action}, function (response) {
if ($.isNumeric(response)) {
$('li#' + id)
.find('div.upvote')
.removeClass('selected');
$('div.vote-tally span.num').html(response);
}
});
} else {
var vote_action = 'vote';
$.post('/b/vote/', {id: id, type: vote_type, action: vote_action}, function (response) {
if ($.isNumeric(response)) {
$('li#' + id)
.find('div.upvote')
.addClass('selected');
$('div.vote-tally span.num').html(response);
}
});
}
});
}

最佳答案

$(document).ready(function () {
"use strict";

$('div.upvote').click(function () {
var id = $(this).parents('li.book').attr('id');
var vote_type = 'up';


if ($(this).hasClass('selected')) {
var vote_action = 'recall-vote';
(function (response) { // skip ajax, just call success callback
if ($.isNumeric(response)) {
$('li#' + id)
.find('div.upvote')
.removeClass('selected');
$('div.vote-tally span.num').html(response);
}
}(123)); // fake numeric response
} else {
var vote_action = 'vote';
(function (response) {
if ($.isNumeric(response)) {
$('li#' + id)
.find('div.upvote')
.addClass('selected');
$('div.vote-tally span.num').html(response);
}
}(123));
}
});
}) // ) missed

HTML:

<li id="li-id" class="book">
<div class="upvote selected">upvote</div>
</li>​

使用假数据一切正常。因此,您遇到非数字响应或错过的问题 )

关于javascript - removeClass() 不会删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10061624/

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