作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究这个问题,并一直在谷歌上搜索解决方案。
背景我想删除“customBox禁用”类并添加“customBox”类。我尝试了几种方法; .removeClass("customBoxDisabled").addClass("customBox")、toggle(....,....) 以及下面的但无济于事。问题是代码确实更改为新类,但程序一结束它就恢复了。但为什么?有专家吗?
$(document).on("click", "#btnStep2", function() {
alert($("#step2").children().children().children().children().eq(1).children().eq(1).attr("class"));
if ($("#step2").children().children().children().children().eq(1).children().children().is(':disabled')) {
alert("disabled");
if ($("#step2").children().children().children().children().eq(1).children().eq(1).hasClass("customBox disabled")) {
$("#step2").children().children().children().children().eq(1).children().eq(1).removeClass();
alert($("#step2").children().children().children().children().eq(1).children().eq(1).attr("class"));
$("#step2").children().children().children().children().eq(1).children().eq(1).addClass("customBox disabled");
alert($("#step2").children().children().children().children().eq(1).children().eq(1).attr("class"));
return false;
}
} else {
alert("enabled");
if ($("#step2").children().children().children().children().eq(1).children().eq(1).hasClass("customBox")) {
$("#step2").children().children().children().children().eq(1).children().eq(1).removeClass();
alert($("#step2").children().children().children().children().eq(1).children().eq(1).attr("class"));
$("#step2").children().children().children().children().eq(1).children().eq(1).addClass("customBox");
alert($("#step2").children().children().children().children().eq(1).children().eq(1).attr("class"));
return false;
}
}
return false;
});
最佳答案
首先,您需要替换所有这些 .children 方法。只需使用 find() 方法即可使代码更清晰、更易于阅读。
其次,我想向您澄清“disabled”和“customBox”是两个单独的类。通过查看问题的描述,您只想切换“禁用”类别。 “customBox”类不必执行任何操作。
毕竟,请尝试下面的代码。希望有帮助
$(document).on("click", "#btnStep2", function() {
var custom_box = $("#step2").find(".customBox");
if (custom_box.length > 0){
custom_box.toggleClass("disabled");
}
});
快乐编码:)
关于jquery - AddClass和RemoveClass恢复到原来的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588027/
如何使用 Blazor 在 Linux 平台下运行 Desktop 程序 本文将讲解如何使用 Blazor 运行跨平台应用,应用到的技术有以下几点 Blazor
低并发的友友们好,我是闪客。 Lambda 表达式非常方便,在项目中一般在 stream 编程中用的比较多。 List<Student> studen
我是一名优秀的程序员,十分优秀!