我正在尝试使用 jquery 动态加载和卸载内容。
.load() 工作得很好,但出于某种原因我不能用 .unload() 做同样的事情。我不确定我做错了什么。
这是我的代码
$( document ).ready( function() {
$( 'a.dynamicLoad' ).click( function( e ) {
e.preventDefault(); // prevent the browser from following the link
e.stopPropagation(); // prevent the browser from following the link
$( '.content-wrap' ).load('projects/project1.html');
});
$('#back').click(function(){
$( '.content-wrap' ).unload('projects/project1.html');
});
});
最佳答案
jquery中的unload函数不用于此目的。它用于设置要发生的窗口卸载事件,例如退出页面时。
https://api.jquery.com/unload/
The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.
如果您只需要清除元素,将 innerHTML 设置为空就足够了。
$( '.content-wrap' ).html('');
参见 http://jsfiddle.net/dm7hhnjc/
关于 OP,jquery 中的这种加载和卸载范围可能会使任何人意外误入歧途,因为除非您已经熟悉 window.onunload,否则命名方案本身就很困惑
关于javascript - jQuery .unload() 不起作用,但 .load() 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795409/