问题是当我使用警报时,toggleClass 会起作用,否则不会。
这是我的代码
$(testParentSpan).click(function(){
$(this).toggleClass("parent1");
alert("test is click");
});
其中 testParentSpan 是存储整个范围标签的变量
var testParentSpan = htmlLabelElementObj.parentElement.parentElement.parentNode.parentNode.parentNode.firstChild;
CSS
span.parent1 span.dynatree-icon {
background-image: url('closefolder.png') !important;
}
/*span.parent1[class~='dynatree-expanded'] */
span.dynatree-expanded[class~=parent1] span.dynatree-icon {
background-image: url('openfolder1.png') !important;
}
我试过了
$(testParentSpan).bind('click','.class',function(){
$(this).toggleClass("parent1");
alert("test is click");
});
效果相同,没有变化。当我使用警报时它有效,否则无效。
最佳答案
想到的唯一合理的事情是,您可能有一些代码可以更改 css 类或执行其他一些 dom 操作,并且该代码会延迟它在警报场景中的执行 - 警报会阻止控制流。根据问题中提供的信息,我只能建议进行调试,以便跟踪生成此竞争条件的代码发生的位置......
关于javascript - 当我使用警报时,toggleClass 正在工作,否则不会。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23752854/