JQuery get / load不能解析新的DOM [英] JQuery get/load cannot parse new DOM

查看:245
本文介绍了JQuery get / load不能解析新的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过AJAX加载另一个页面内容。它加载内容,但我有解决新内容的问题。



这是我的代码

  $ .get(urlPath,function(content){
var newDom = $(content);
var newTitle = newDom.find(title ;
console.log(newDom);
console.log(newDom.find(CLASS.MAIN_CONTENT));
console.log(newTitle);
$(CLASS.MAIN_CONTENT ).replaceWith(newDom.find(CLASS.MAIN_CONTENT));
});

这不起作用,我从请求中获取有效的内容,但仍然无法从新的DOM
这是以 $(content)解析的输出





这里是内容的原始输出 console.log(content) / p>



可能是错的,为什么不

解决方案

首先,请注意,使用 $()解析HTML将丢弃其中的一些,因为 $()希望解析正文内容,而不是一个完整的文档。所以看起来像它们应该嵌套在结构中的元素最终可能在jQuery对象的顶层。例如:



  var content = $(&do;!doctype html> +< html>+< head>< title>示例< / title>< / head>+< body>+< p> Hi there< / p> < / body>+< / html>); console.log(content.length); // 2content.each(function(){console.log(this.tagName); // TITLE,P});  

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< ; / script>  



code>在内部中显示jQuery对象中的顶级元素,但不会看到顶级元素本身,只是其后代。如果与 CLASS.MAIN_CONTENT 选择器匹配的元素位于顶层,则不会被 find 找到。您需要过滤器。如果它可能位于任何一个位置,您需要组合 find 过滤器

  var mainContent = newDom.find(CLASS.MAIN_CONTENT).add(newDom.filter(CLASS.MAIN_CONTENT)); 


I'm trying to load another page content via AJAX. It loads the content, but I have a problem with parsing new content.

Here is my code

  $.get(urlPath, function (content) {
                var newDom = $(content);
                var newTitle = newDom.find("title").text();
                console.log(newDom);
                console.log(newDom.find(CLASS.MAIN_CONTENT));
                console.log(newTitle);
                $(CLASS.MAIN_CONTENT).replaceWith(newDom.find(CLASS.MAIN_CONTENT));
            });

And this doesn't work, I get the valid content from request but still cannot get any element from new DOM Here is output of parsed with $(content)

And here is the raw output of the content console.log(content)

What can be wrong, why this doesn't work ?

解决方案

First, note that using $() to parse that HTML will throw away some of it because $() expects to be parsing body content, not a full document. So elements that seem like they should be nested within the structure may end up at the top level of the jQuery object. For instance:

var content = $(
  "<!doctype html>" +
  "<html>" +
  "<head><title>Example</title></head>" +
  "<body>" +
  "<p>Hi there</p>" +
  "</body>" +
  "</html>"
);
console.log(content.length); // 2
content.each(function() {
  console.log(this.tagName); // TITLE, P
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

find looks within the top-level elements in a jQuery object, but doesn't look at the top-level elements themselves, just their descendants. If the element matching your CLASS.MAIN_CONTENT selector is at the top level, it won't be found by find. You'd want filter instead. If it may be in either location, you want a combination of find and filter:

var mainContent = newDom.find(CLASS.MAIN_CONTENT).add(newDom.filter(CLASS.MAIN_CONTENT));

这篇关于JQuery get / load不能解析新的DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆