加载页面使用Ajax分页后重新初始化其他JavaScript函数(reinitialize othe

2019-07-03 16:57发布

道歉,这里一共有福利局。 我如何可以加载其他插件,并让其他不同的脚本函数加载一个Ajax生成的页面后? 这是我的CURENT代码:

jQuery(document).ready(function($) {
var $mainContent = $("load-content"),
siteUrl = "http://" + top.location.host.toString(),
url = ''; 

$(document).delegate("a[href^='"+siteUrl+"']:not([href*='/wp-admin/']):not([href*='/wp-login.php']):not([href$='/feed/'])", "click", function() {

if($.browser.msie){
var myie="/"+this.pathname;
location.hash = myie;
//alert(location.hash);

}else{

location.hash = this.pathname;
}
return false;
});


$("#searchform").submit(function(e) {

$search = $("#s").val();
$search = $.trim($search);
$search = $search.replace(/\s+/g,'+');

location.hash = '?s='+$search;
e.preventDefault();
});

$(window).bind('hashchange', function(){
url = window.location.hash.substring(1);
if (!url) {
return;
}

url = url + " #content";

$('html, body, document').animate({scrollTop:0}, 'fast');

$mainContent.fadeOut(500, function(){$('#content').fadeOut(500, function(){
$("#loader").show();});}).load(url, function() {
$mainContent.fadeIn(500, function(){
$("#loader").hide(function(){ $('#content').fadeIn(500);});});});
});
$(window).trigger('hashchange');


});

在页面上嵌入的对象如何能留住他们的功能? 主要视频,幻灯片以及使用JavaScript像其他媒体

视频JS(HTML5视频播放器)

VIMEO

投资组合幻灯片为WordPress

Answer 1:

当加载AJAX生成的标记也不会保留它之前,必须的功能。 在你上面的例子,你在初始化时的事情在DOM准备好了要执行。 为了确保任何插件,等等,你执行你需要重新初始化他们Ajax请求后运行。

鉴于上面的代码示例中,我建议你一点点调整。 例如,你可以创建一个函数调用init ,您可以调用初始化某些插件:

function init () {
    $("#plugin-element").pluginName();
}

jQuery(document).ready(function () {
    // Initialise the plugin when the DOM is ready to be acted upon
    init();
});

然后,在这之后,你Ajax请求的成功回调,可以再次调用它,这将重新初始化插件:

// inside jQuery(document).ready(...)
$.ajax({
    type: 'GET',
    url: 'page-to-request.html',
    success: function (data, textStatus, jqXHR) {
        // Do something with your requested markup (data)
        $('#ajax-target').html(data);            

        // Reinitialise plugins:
        init();
    },
    error: function (jqXHR, textStatus, errorThrown) {
        // Callback for when the request fails
    }
});


文章来源: reinitialize other javascript functions after loading a page with ajax pagination