I have a div. Inside that div I have multiple elements. Some elements have the class 'myClass'. I also have a button. When clicked, a foreach loop runs for each element that has the class myClass within the div. An ajaxCall is send for each element. The text color of those elements are black by default. In case of success of the ajax call. I would like to remove the class classBlackFont and add the one classGreenFont. I tried the following code which is unfortunately not switching the classes eventhough the ajax call succeeded.
$("#someDiv .myClass").each(function() {
var ajaxData = "myAjaxData";
$.ajax({
type: "POST",
url: "somefile.php",
data: ajaxData,
success: function(data) {
$(this).removeClass('classBlackFont').addClass('classGreenFont');
}
});
});
博客内容涉及在JavaScript中处理DOM元素,特别是通过Ajax调用成功后改变具有特定类别的元素的类名。作者遇到的问题是,尽管Ajax请求成功,但元素的类名没有按预期切换。代码示例中,作者尝试遍历具有'myClass'类的元素,并在Ajax成功回调中移除'classBlackFont'并添加'classGreenFont',但未能生效。

被折叠的 条评论
为什么被折叠?



