单击 A 类按钮时,将 A 类替换为 B 类。单击 B 类按钮时,我需要执行一个操作。
它正确地应用/删除了该类,但由于某种原因它不会选择 B 类?
$(".changebut").click(function () {
$("input[type=text]").fadeOut("fast", function () {
$(".changebut").animate({
width: "100%"
}, 200).val("Submit again").removeClass("changebut").addClass("submitbut")
})
}),
$(".submitbut").click(function () {
alert('it worked!')
})
fiddle http://jsfiddle.net/B9RuM/1/
最简单的解决方案是使用事件委托而不是直接绑定:
$(document).on('click', '.changebut', function () {
$("input[type=text]").fadeOut("fast", function () {
$(".changebut").animate({
width: "100%"
}, 200).val("Submit again").removeClass("changebut").addClass("submitbut")
});
})
$(document).on('click', '.submitbut', function () {
alert('it worked!')
})
最好的解决方案是直接绑定点击事件并检查当前类:
$(".changebut").on('click', function () {
if($(this).is('.changebut')){
$("input[type=text]").fadeOut("fast", function () {
$(".changebut").animate({
width: "100%"
}, 200).val("Submit again").removeClass("changebut").addClass("submitbut")
});
}else{
alert('i have an other class');
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)