简体   繁体   English

Internet Explorer中的jQuery无法解析字符串html

[英]JQuery in internet explorer cant parse a string html

I am trying to parse an HTML string in Internet Explorer using jQuery, based on: Parsing HTML String with Ajax/jQuery . 我试图基于jQuery在Internet Explorer中解析HTML字符串 :基于Ajax / jQuery解析HTML字符串 here is the code: 这是代码:

alert(result);
alert($(result));

The first alert prompts the HTML, but second alert just gives me Object. 第一个警报提示HTML,但是第二个警报只是给我Object。 On firebug lite console it gives me a blank object: [] !! 在Firebug lite控制台上,它给了我一个空白的对象:[]!

console.log(result)
console.log($(result))

result is a big XHTML code, that is received through an AJAX call. 结果是一个很大的XHTML代码,它是通过AJAX调用接收的。 The same code works on Firefox.. 相同的代码可在Firefox上运行。

Anyone has any idea why this could be happening? 任何人都不知道为什么会这样吗? Your help is much appreciated..! 非常感谢您的帮助..!

Without seeing the HTML code, it's hard to say, but I would have to guess that you're hitting the "Unknown runtime error" that occurs when you're invalidly trying to put an element where it's not allowed. 没有看到HTML代码,很难说,但是我不得不猜测您是在无效地尝试将元素放置在不允许的位置遇到的“未知运行时错误”

You see, jQuery builds the DOM from (X)HTML by creating a detached element and applying the (X)HTML to that element's innerHTML property. 您会看到,jQuery通过创建一个分离的元素并将(X)HTML应用于该元素的innerHTML属性,从(X)HTML构建DOM。 FWIW, neither browsers care if you're passing XHTML or HTML, unless you're serving an XHTML mime type that would be giving you bigger problems. FWIW,除非您提供的XHTML MIME类型会给您带来更大的问题,否则浏览器都不关心您是要传递XHTML还是HTML。 If you consider the following plain JS code: 如果考虑以下普通的JS代码:

var p = document.createElement("p");
p.innerHTML = "<li>Test</li>";

Internet Explorer with throw a very unhelpful "Unknown runtime error" , whereas Firefox will do its best to salvage the invalid HTML. Internet Explorer会抛出一个非常无用的“未知运行时错误” ,而Firefox会尽力挽救无效的HTML。

I would recommend putting your XHTML through the W3C Validator and checking it for validation errors. 我建议您将XHTML放入W3C验证程序中,并检查是否存在验证错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM