在后退按钮上单击执行功能。

Tan*_*ova 5 javascript jquery

在document.ready事件中,我有一个函数收集具有属性“wordNum”的 html 控件,我发出一个 AJAX 请求,该请求返回每个控件的一些描述,然后我使用innerhtml返回的描述设置这些控件及其属性。

问题是,如果用户单击后退按钮,这个功能当然不会执行,但我需要它。

代码:

$(document).ready(function () {
    Translate();
});
function Translate() {
    var list = new Array();
    $("*[wordNum]").each(function () {
        var endRes = {
            ControllerName: this.id,
            WordNumber: this.getAttribute("wordNum")
        };
        list.push(endRes);
    });
    jQuery.ajax({
        url: ' @Url.Action("Translate")',
        contentType: "application/json",
        dataType: "json",
        data: {
            List: JSON.stringify(list)
        },
        traditional: true,
    }).done(function (data) {
        $.each(data, function (key, val) {
            $("*[wordNum]").each(function () {
                if (this.id == val.ControllerName) {
                    this.innerHTML = val.Description;
                }
            });
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能使后退按钮单击再次执行此功能Translate。

Gen*_*tto 4

您可以使用 popstate 事件来触发该事件:

$(window).on("popstate", function (event, state) {
    // Here comes the code to execute when the back button is pressed
} );
Run Code Online (Sandbox Code Playgroud)