我有一个较旧的脚本,需要对其进行修改以使其与更多加载功能一起使用。但是我遇到了替换.bind()和.unbind()的问题。脚本本身会在几个位置添加类,然后在单击和取消单击某个元素时将其删除。
当我尝试用.on()和.unbind()用.off()替换.bind()时,脚本失败。有什么想法吗?
jQuery(document).ready(function($) {
//alert('bbbb')
// cache the floating element
var goToTop = $(".sendids");
// bind to the window's scroll event to move the element with the user
$(window).on("scroll", function() {
goToTop.stop().animate({
"marginTop": ($(window).scrollTop()) + "px"
}, "slow");
});
$(".add-job").bind("click", function(e) {
temp = "#s" + $(this).parent().parent().attr("id");
//alert('show id: ' + temp);
$(this).toggleClass("check");
if ($(temp).length != 0) {
// remove ID from selected ID list
$("div#" + temp).remove();
temp = null;
if ($("#selectedids").children().size() == 0) {
$(".sendids").removeClass("hover");
$(".sendids-hor").removeClass("hover");
}
} else {
// add ID to selected ID list
$("#selectedids").append('<div id="' + $(this).parent().parent().attr("id") + '"></div>');
$(".sendids").addClass("hover");
$(".sendids-hor").addClass("hover");
}
if ($("#selectedids").children().size() > 0) {
//alert('aaaaaa');
$(".sendids,.sendids-hor").bind("click", function() {
var allids = '';
var clickeditem = $(this);
$("#selectedids").contents().each(function(index) {
if (index > 0) {
allids += ",";
}
allids += $(this).attr("id");
});
if ($("#workshops").has(clickeditem).length > 0) {
window.location = "/mail-workshop-ids?ids=" + allids;
} else {
//send selected IDs to form page;
window.location = "localhost/job-applicant-information-request?ids=" + allids;
}
});
} else {
//alert('bbbb')
$(".sendids").unbind("click");
}
});
});
我也尝试通过文档范围内的.on(“ click”)事件来实现此目的,但首先我需要替换.bind和.unbind