加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在使用ajax分页加载页面后重新初始化其他javascript函数

发布时间:2020-12-16 01:34:37 所属栏目:百科 来源:网络整理
导读:道歉,这里有一个新的.如何加载其他插件,并在加载ajax生成的页面后让其他单独的脚本运行?这是我的代码: jQuery(document).ready(function($) {var $mainContent = $("load-content"),siteUrl = "http://" + top.location.host.toString(),url = ''; $(docum
道歉,这里有一个新的.如何加载其他插件,并在加载ajax生成的页面后让其他单独的脚本运行?这是我的代码:
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的媒体

video js (html5 video player)

VIMEO

portfolio slideshow for wordpress

当您加载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,errorThrown) {
        // Callback for when the request fails
    }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读