使用.html()时,jQuery是否会从字符串中剥离一些html元素? [英] Does jQuery strip some html elements from a string when using .html()?

查看:168
本文介绍了使用.html()时,jQuery是否会从字符串中剥离一些html元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含完整html页面的var,包括head,html,body等等。当我将这个字符串传递给.html()函数时,jQuery去掉了所有这些元素,比如body,html,头等,这是我不想要的。



我的数据var包含:

 < HTML> 
< head>
< title>无标题文档< / title>
< / head>
< body>
< / body>
< / html>

然后我的jQuery是:

  // data是一个完整的html文档字符串
data = $('< div />')。html(data);
// jQuery创建我的文档字符串!
alert(data.find('head')。html());

我需要操作完整的html页面字符串,以便我可以返回元素中的内容。我想用jQuery做到这一点,但似乎所有的方法,append(),prepend()和html()都尝试将字符串转换为dom元素,这会删除完整html页面的所有其他部分。



有没有另外一种方法可以做到这一点?我会很好用另一种方法。我的最终目标是在我的字符串中找到某些元素,所以我认为jQuery会是最好的,因为我很习惯它。但是,如果要修剪和删除部分字符串,我将不得不寻找其他方法。



想法?

$ b $在经过几次快速测试后,我认为这种行为不是由jQuery引起的,而是由浏览器引起的。



由于您可以轻松验证自己(DEMO http://jsbin.com/ocupa3

  var data =< html>< head>< title>无标题文档< / title>< / head> ;<身体GT;< p为H.试验< / p>< /体>< / HTML>中; 
data = $('< div />')。html(data);
alert(data.html());

在不同浏览器中产生不同结果

Opera 10.10

 < HEAD>< TITLE>无标题文档< / TITLE>< / HEAD>< P>测试< ; / P> 

FF 3.6

 < title>无标题文档< /标题>< p>测试< / p> 

IE6

 < P>试验< / P> 

所以这与jQuery无关,它是在插入整个页面时剥离一些标签的浏览器div内的html字符串。但是您需要逐步完成 html()的整个jQuery代码。您需要为所有浏览器都这样做,因为jQuery尝试完成它的工作有几种不同的方式。






我建议您使用iframe(可能隐藏)进行调查,并将iframe内容设置为您所拥有的html字符串。但请注意,摆弄iframe并以编程方式更改其内容并非易事。也有不同的浏览器相关的怪癖和时间问题。


I have a var that contains a full html page, including the head, html, body, etc. When I pass that string into the .html() function, jQuery strips out all those elements, such as body, html, head, etc, which I don't want.

My data var contains:

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

Then my jQuery is:

// data is a full html document string
data = $('<div/>').html(data);
// jQuery stips my document string!
alert(data.find('head').html());

I am needing to manipulate a full html page string, so that I can return what is in the element. I would like to do this with jQuery, but it seems all of the methods, append(), prepend() and html() all try to convert the string to dom elements, which remove all the other parts of a full html page.

Is there another way that I could do this? I would be fine using another method. My final goal is to find certain elements inside my string, so I figured jQuery would be best, since I am so used to it. But, if it is going to trim and remove parts of my string, I am going to have to look for another method.

Ideas?

解决方案

After a few quick tests it seems do me that this behavior isn't caused by jQuery but instead by the browser.

As you can easily verify yourself (DEMO http://jsbin.com/ocupa3)

var data = "<html><head><title>Untitled Document</title></head><body><p>test</p></body></html>";
data = $('<div/>').html(data);
alert(data.html());

yields different results in different browsers

Opera 10.10

<HEAD><TITLE>Untitled Document</TITLE></HEAD><P>test</P>

FF 3.6

<title>Untitled Document</title><p>test</p>

IE6

<P>test</P>

so this has nothing to do with jQuery, It's the browsers which strip some tags when you insert a whole html string inside a div. But you would need to step through the whole jQuery code for html() to be sure. And you would need to do that for all browsers as there are several different ways jQuery tries to do it's job.


For a solution I advise you to investigate using an iframe (possibly hidden) and to set that iframe content to the html-string you have. But be aware that fiddling with iframes and changing their content programmatically isn't an easy task either. There are also different browser related quirks and timing issues involved.

这篇关于使用.html()时,jQuery是否会从字符串中剥离一些html元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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