【问题标题】:How to recode the jquery load with ajax get如何使用 ajax get 重新编码 jquery 负载
【发布时间】:2015-01-14 09:25:29
【问题描述】:

现在我正在使用以下代码:

$main.load(href + " main>*");

这样做是加载我给它的页面,并且只加载“main”标签内的内容,包括“main”。我想要做的是在加载时添加进度条,所以我看到只有 .ajax 才有可能,所以我尝试了这个:

$.ajax({
            xhr: function()
            {
                var xhr = new window.XMLHttpRequest();
                //Download progress
                xhr.addEventListener("progress", function(evt){
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;
                        //Do something with download progress
                        console.log(percentComplete);
                    }
                }, false);
                return xhr;
            },
            type: 'POST',
            url: href + " main>*",
            data: {},
            success: function(data){
                //Do something success-ish                                      
            }
        });

但这不一样。那么我该如何去加载一个页面的内容并且只有它是“主要”内容呢?

【问题讨论】:

  • 不太清楚问题是什么。如何使用$.axax获取main
  • @charlietfl 是的,这就是目标。

标签: javascript jquery ajax load


【解决方案1】:

.load() 所做的基本上是用加载了可选过滤器的内容填充所选元素,这基本上会丢弃除与过滤器匹配的所有内容。

...
url: href,
success: function(data){
    var content = $(data).find("main>*");// if main is a child of the body tag will fail, try .filter() instead
    $main.empty().append(content);                              
}
...

【讨论】:

  • 用 .filter() 尝试过,因为它是 body 标签的子标签,但我得到了一个空页面
猜你喜欢
  • 2014-01-11
  • 2020-06-17
  • 2013-08-16
  • 1970-01-01
  • 2021-10-06
  • 2013-01-04
  • 2014-03-12
  • 2011-02-19
  • 2012-02-17
相关资源
最近更新 更多