查找body标签在Ajax HTML响应

编程入门 行业动态 更新时间:2024-10-16 20:26:04
本文介绍了查找body标签在Ajax HTML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在做一个Ajax调用来获取内容并追加像这样这样的内容:

$(函数(){     VAR网站= $('输入')VAL()。     $获得('为file.php',{网站:网站},功能(数据){         马斯= $(数据).find('一个');         mas.map(功能(ELEM,指数){             的div = $(本)。html的();             $('#结果)追加(''+的div +'')。         })     },HTML); });

现在的问题是,当我修改 A 在体我什么也没有(没有错误,只是没有HTML) 。林假设身体是一个就像'一'是标签?我究竟做错了什么?

所以这对我的作品:

MAS = $(数据).find('A');

但这并不:

MAS = $(数据).find('身体');

解决方案

通过一个jQuery对象(即 $(数据)),以解析返回的HTML来获得体标签是注定要失败,我害怕。

原因是,返回数据是字符串(试行的console.log (typeof运算(数据)))。现在,根据 jQuery的文档,从字符串创建一个jQuery对象当包含复杂的HTML标记,标记,如体有可能得到剥离。出现这种情况,因为为了创建对象,HTML标记被实际插入到DOM不能容许这样的其他标签。

从文档:

  

如果一个字符串作为参数传递给$(),jQuery的检查字符串是否它看起来像HTML。

     

[...]   如果HTML比没有属性单个标记更加复杂,因为它是在上述的例子中,元件的实际创建由浏览器的innerHTML机制来处理。在大多数情况下,jQuery的创建一个新的元素,并将元素中传递的HTML代码段的innerHTML属性当参数有(带有可选的结束标记或快速关闭)一个标签 - $(< IMG />)或$(< IMG>),$(< A>< / A>)或$(<一>) - jQuery的创建使用原生的JavaScript的createElement元素( )函数。

     

在复杂的HTML路过时,某些浏览器可能不会产生一个DOM   这完全复制提供的HTML源代码。如所提到的,jQuery的   使用浏览器的S .innerHTML属性来解析通过HTML和   将其插入到当前文档中。在此过程中,一些   浏览器过滤掉某些元素例如< HTML>,<标题>,或   < HEAD>元素。其结果,插入的元件可能不   再通过原始字符串的presentative。

I'm making an ajax call to fetch content and append this content like this:

$(function(){ var site = $('input').val(); $.get('file.php', { site:site }, function(data){ mas = $(data).find('a'); mas.map(function(elem, index) { divs = $(this).html(); $('#result').append('' + divs + ''); }) }, 'html'); });

The problem is that when I change a in body I get nothing (no error, just no html). Im assuming body is a tag just like 'a' is? What am I doing wrong?

So this works for me:

mas = $(data).find('a');

But this doesn't:

mas = $(data).find('body');

解决方案

Parsing the returned HTML through a jQuery object (i.e $(data)) in order to get the body tag is doomed to fail, I'm afraid.

The reason is that the returned data is a string (try console.log(typeof(data))). Now, according to the jQuery documentation, when creating a jQuery object from a string containing complex HTML markup, tags such as body are likely to get stripped. This happens since in order to create the object, the HTML markup is actually inserted into the DOM which cannot allow such additional tags.

Relevant quote from the documentation:

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML.

[...] If the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's innerHTML mechanism. In most cases, jQuery creates a new element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag (with optional closing tag or quick-closing) — $( "< img / >" ) or $( "< img >" ), $( "< a >< /a >" ) or $( "< a >" ) — jQuery creates the element using the native JavaScript createElement() function.

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser"s .innerHTML property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as < html >, < title >, or < head > elements. As a result, the elements inserted may not be representative of the original string passed.

更多推荐

查找body标签在Ajax HTML响应

本文发布于:2023-08-03 12:48:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   body   HTML   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!