【问题标题】:Javascript on AJAX with jquery带有 jquery 的 AJAX 上的 Javascript
【发布时间】:2012-07-21 21:38:29
【问题描述】:

-

我该如何处理它,以便在 Ajax 调用之后让 Javascript 正常工作?

在我的页面上 (wordpress) - 我在起始页上有所有帖子。我已经内置了一个过滤系统,用户可以使用它过滤特定内容之后的帖子 - 例如“最新、最多查看、突出显示”

我使用下面的这个函数来使这个过滤器工作:

 <script type="text/javascript">
    $(document).ready(function(){
    $.ajaxSetup({cache:false});
    $("#new").click(function(){

        var post_id = $(this).attr("rel")
        $(".postbox_wrapper").html('<span class="filter_posts">
<img src="<?php bloginfo ('template_directory'); ?>/images/287.gif"></span>');
        $(".postbox_wrapper").load(jQuery(this).attr("href") + " .postbox_wrapper")
        return false;
    });
});

访问者点击“新建”按钮,帖子被过滤 - 只有最新的帖子显示。但是我使用 Javascript 来统计 disqus、惰性图像加载器等的帖子计数。这些 JS 文件不会加载到过滤后的帖子上。 (taxonomy.php)

这里是例如一个 JS 文件,这对于使过滤后的帖子可以使用惰性加载器是必要的:

http://myurl/../plugins/bj-lazy-load/js/bjll.min.js?ver=0.4.0

问题是:我怎么知道在上面的函数中,也可以在新加载的“过滤”页面上使用 JS 脚本。

有点像这样:Use Ajax() function in Jquery to load PART of an external page into div

但我没有让它工作......

非常感谢!

【问题讨论】:

  • 您能否重新表述您的问题(更准确一点)?我没有得到你想要达到的目标。

标签: jquery ajax


【解决方案1】:

我不确定我是否理解您的问题。 您是否尝试在 div 中加载特定内容后加载脚本?

如果是这样试试:

$(function(){
    $.ajaxSetup({cache:false});

    $("#new").click(function(){
        var $this = $(this);
        var $PostboxWrapper = $(".postbox_wrapper");

        var post_id = $this.attr("rel")

        //I think you want load something like a loading .gif in taht point. Right?
        $PostboxWrapper.html(
            "<span class='filter_posts'>" + 
            "   <img src='<?php bloginfo ('template_directory'); ?>/images/287.gif'>" +
            "</span>"
        );

        $(".postbox_wrapper").load(jQuery(this).attr("href"), function()
        {
            $.getScript(
                "http://yourUrl/../plugins/bj-lazy-load/js/bjll.min.js?ver=0.4.0",
                function(data, textStatus, jqxhr) {
                    console.log('Load was performed.');
                }
            );
        })
        return false;
    });
});

【讨论】:

  • 嗨豪尔赫 - 感谢您的回复!对,就是这样。我尝试将 js 加载到 ajax 加载页面中。我正在尝试您的代码 - 但它会将整个页面加载到 div 中
  • js 应该只是传递给新加载的页面......我猜这个 getScript 的东西是正确的方法。但它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2015-09-29
  • 2012-12-26
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多