javascript - jQuery .unload() 不起作用,但 .load() 起作用

标签 javascript jquery

我正在尝试使用 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/

相关文章:

javascript - 当记录超过一个时,无法禁用文本字段

javascript - 动态更新 'nested' javascript对象

javascript - Chart.js:组合线和条数据

javascript - 如何动态地向类元素添加 Font Awesome 图标?

jquery - 多向滚动网站

javascript - 如何在 javascript 中使用 map() 从 javascript 对象中提取数据?

javascript - 在页面顶部插入内容而不滚动

c# - 使用 JSON 的级联下拉列表不会带来新记录

javascript - 多个 div 到一个 ajax 页面

javascript - Jquery - 如何访问单击以运行函数的元素?