在后退按钮上单击执行function。

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

问题是如果用户单击后退按钮,当然这个function没有执行但我需要它。

码:

 $(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; } }); }); }); } 

那么如何在后退按钮上单击再次执行此functionTranslate

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

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