Javascript 如何在修改的元素上重新绑定事件

Javascript 如何在修改的元素上重新绑定事件,javascript,Javascript,我的问题是: 我有一个应答器,在那里我设置了一个onclick事件。 当我完成我的叛逆时,我想不起来了 以下是我的部分代码: $('.editAction').click(function (event) { //Some stuff handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr); //Call to another function return false; //Do not redirect to URL }); f

我的问题是:

我有一个应答器,在那里我设置了一个onclick事件。 当我完成我的叛逆时,我想不起来了

以下是我的部分代码:

$('.editAction').click(function (event) {
//Some stuff
handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr);
//Call to another function
return false; //Do not redirect to URL
});

function handleEditThemeAjax(bouton, themeId, nomEn, nomFr) {
     $(bouton).unbind("click");
     $(bouton).removeClass("editAction"); //To not get another call to the parent function

//unset url to avoid being redirected
bouton.removeAttribute("href");

//Some stuff and declaration
$(bouton).click(function () {
    //AJAX Handle
    $.ajax({
        type: "POST",
        url: "{{ path('editThemeAjax') }}", //Twig
        data: {
            "nomFr": newNomFr,
            "nomEn": newNomEn,
            "themeId": themeId
        },
        success: function (data) {
            setInputToText(tdFr, newNomFr);
            setInputToText(tdEn, newNomEn);
            Materialize.toast(data, 2000, 'rounded');
        },
        error: function (data) {
            console.log(data);
            Materialize.toast("Une erreur est survenue lors de l'édition de la thématique");
        }
    });
}

//Let's put the button to it initial state

var tr = tdEn.parentElement;
lineButton = tr.childNodes[7];

//The button
var boutonDone = lineButton.childNodes[1];
$(boutonDone).removeClass("green");
$(boutonDone).addClass('blue editAction');
boutonDone.childNodes[1].innerHTML = "mode_edit";

// Clone the button to remove all previous event
var clone = boutonDone.cloneNode();
while (boutonDone.firstChild) {
    clone.appendChild(boutonDone.lastChild);
}

//replace it
boutonDone.parentNode.replaceChild(clone, boutonDone);

clone.href = url;
//Set the url again

//Now, if i click on my button again, nothing append. 
//I would like that if i clicked again, my first function is executed (the "$('.editAction').click(...)")

});
return false; //avoid being redirected
}
我能做些什么来修复它


提前感谢您的帮助

我建议将您的单击事件设置器包装到一个函数中,并在实际需要时再次调用它

var set_editAction_click_events = function (themeId, nomThemeEn, nomThemeFr) {
    $('.editAction').click(function (event) {
        //Some stuff
        handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr);
        //Call to another function
        return false; //Do not redirect to URL
    });
}

// ... your code ...

//The button
var boutonDone = lineButton.childNodes[1];
$(boutonDone).removeClass("green");
$(boutonDone).addClass('blue editAction');
boutonDone.childNodes[1].innerHTML = "mode_edit";

// Set click events again
set_editAction_click_events(themeId, nomThemeEn, nomThemeFr);
或者,如果您不想在函数中包装代码:

$(boutonDone).click(function() {
    handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr);
    //Call to another function
    return false; //Do not redirect to URL
});

我不清楚你到底想要什么。我只明白,点击事件时,您再次设置点击事件。然后?然后我设置了另一个事件,所以我必须禁用第一个。在触发第二个事件后,我想启用第一个事件,但我不能。然后将其绑定到第二个事件触发器上!!!你什么意思?我不明白,将新按钮设置为classes
blue editAction
不会神奇地将onclick事件附加到它上-您需要重新设置它。