[英]jquery addClass adding a class to div but also removing it instantly
Hi i am trying to remove a class from div
(hide class) and want to add a class (show class). 嗨,我正在尝试从
div
删除一个类(隐藏类),并想添加一个类(显示类)。 i am using addClass
and removeClass
for this but this behaving very funny. 我为此使用
addClass
和removeClass
,但这表现非常有趣。
$('#table-csv').removeClass('hideDiv')
it is removing class from div
but adding it again instantly. 它正在从
div
删除类,但立即又添加了它。 I am doing it in ajax
success response from Django view. 我正在从Django视图的
ajax
成功响应中执行此操作。
I tried lots of solution available on Stackoverflow and other websites but no luck. 我尝试了很多Stackoverflow和其他网站上可用的解决方案,但没有运气。
css
classes: css
类:
.hideDiv{
display: none;
}
.showDiv{
display: block;
}
my script: 我的脚本:
$.ajax(
{
type:"POST",
url: "/graph/upload-csv/",
headers: {'X-CSRFToken': '{{ csrf_token }}'},
data:{
input: input,
output: output,
},
success: function( data )
{
$('#table-csv').addClass('showDiv').removeClass('hideDiv');
}
})
when I am trying to removeClass
on simple button click then it is working fine. 当我尝试在简单按钮上单击
removeClass
,它工作正常。 problem is only when I am doing it in ajax success response 问题只是当我在ajax成功响应中执行此操作时
I know I will get lots of down votes for this question but I need help. 我知道这个问题我会得到很多反对,但我需要帮助。
You may not need two separate class one to hide another to show . 您可能不需要两个单独的类来隐藏另一个要显示的类。 Instead create a single class
而是创建一个类
.hide{
display:none;
}
When required to hide the element do $(elementSelector).addClass('hide')
and when required to show do $(elementSelector).removeClass('hide')
当需要隐藏元素时,执行
$(elementSelector).addClass('hide')
;当需要显示$(elementSelector).removeClass('hide')
时,执行$(elementSelector).removeClass('hide')
Insert in the DOM the code from the AJAX call and after remove the class in the success
function : 将AJAX调用中的代码插入DOM中,并在
success
函数中删除该类后:
$.ajax({
...
success : function (data) {
$("#div").html(data).find('#table-csv').addClass('showDiv').removeClass('hideDiv');
},
});
My suggestion: make some console.log
for the 'data' in the success function to test the response. 我的建议:在成功函数中为“数据”创建一些
console.log
以测试响应。
Do you declare the hide class inline like this: 您是否像这样声明内联的hide类:
$('#table-csv').addClass('showDiv').removeClass('hideDiv');
.hideDiv { display: none; } .showDiv { display: block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="hideDiv"> test </div>
That doesn't seem to work. 这似乎不起作用。 Try to remove it then and add the class trough javascript.
然后尝试将其删除,然后添加通过JavaScript的类。
$('#table-csv').addClass('hideDiv'); $('#table-csv').addClass('showDiv').removeClass('hideDiv');
.hideDiv { display: none; } .showDiv { display: block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> test </div>
I think when you are using ajax
you can not access UI thread, it seems they have different thread, for this reason an easy way is saving dom
element to a variable and then manipulate it in ajax
success like, in the other words success
event won't access dom
elements or another UI objects truly : 我认为当您使用
ajax
您无法访问UI线程,似乎它们具有不同的线程,因此,一种简单的方法是将dom
元素保存到变量中,然后像在ajax
成功中那样操作它,换句话说, success
事件赢得了不能真正访问dom
元素或其他UI对象:
var myElement = $('#table-csv');
$.ajax(
{
type:"POST",
url: "/graph/upload-csv/",
headers: {'X-CSRFToken': '{{ csrf_token }}'},
data:{
input: input,
output: output,
},
success: function( data )
{
myElement.addClass('showDiv').removeClass('hideDiv');
}
})
or this may because of calling removeClass
and addClass
successively, this is better to call them separately like : 或者这可能是因为先后调用
removeClass
和addClass
,所以最好像这样分别调用它们:
$.ajax(
{
type:"POST",
url: "/graph/upload-csv/",
headers: {'X-CSRFToken': '{{ csrf_token }}'},
data:{
input: input,
output: output,
},
success: function( data )
{
$('#table-csv').addClass('showDiv');
},
stop: function( data )
{
$('#table-csv').removeClass('hideDiv');
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.