我正在使用jquery构建一个拖放应用程序。所有的功能都正常工作,但我使用html.change来启用脚本以删除项目。我现在遇到的问题取决于我在应用程序中对HTML做了多少更改,导致应用程序速度变慢。试图阻止jquery函数循环
这是Javascript代码我使用
$(function() {
$('html').change(function() {
$(".checkdrop").change(function() {
var checkdrop = $(this).closest('.labelDrop').find('.checkdrop').val();
var textul = $(this).closest('.labelDrop').find('.textUL');
var input = '<input type="text" placeholder="Value" class=""><br>';
if (checkdrop >= 3) {
$(this).closest('.labelDrop').find('.three').css("display", "block");
$('th.three').css("display", "table-cell")
}
if (checkdrop >= 4) {
$(this).closest('.labelDrop').find('.four').css("display", "block");
$('th.four').css("display", "table-cell")
}
if (checkdrop >= 5) {
$(this).closest('.labelDrop').find('.five').css("display", "block");
$('th.five').css("display", "table-cell")
}
if (checkdrop >= 6) {
$(this).closest('.labelDrop').find('.six').css("display", "block");
$('th.six').css("display", "table-cell")
}
if (checkdrop >= 7) {
$(this).closest('.labelDrop').find('.seven').css("display", "block");
$('th.seven').css("display", "table-cell")
}
if (checkdrop >= 8) {
$(this).closest('.labelDrop').find('.eight').css("display", "block");
$('th.eight').css("display", "table-cell")
}
if (checkdrop == 2) {
$(this).closest('.labelDrop').find('.three,.four,.five,.six,.seven,.eight').css("display", "none").val("")
}
if (checkdrop == 3) {
$(this).closest('.labelDrop').find('.four,.five,.six,.seven,.eight').css("display", "none").val("")
}
if (checkdrop == 4) {
$(this).closest('.labelDrop').find('.five,.six,.seven,.eight').css("display", "none").val("")
}
if (checkdrop == 5) {
$(this).closest('.labelDrop').find('.six,.seven,.eight').css("display", "none").val("")
}
if (checkdrop == 6) {
$(this).closest('.labelDrop').find('.seven,.eight').css("display", "none").val("")
}
if (checkdrop == 7) {
$(this).closest('.labelDrop').find('.eight').css("display", "none").val("")
}
});
});
});
如果我把在“checkdrop”结束警报就会被触发的次数相同数量的变化,我已经量完成HTML。
有没有办法让这更容易,并防止代码循环?
在此先感谢。
因为您保持绑定事件....为什么你有html更改()? – epascarello
许多改进。最重要的是:不要重新加入处理程序。而不是所有的if语句,使用开关或其他if。 –