Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery('data')返回“data”中包含缺失数据的html片段_Javascript_Jquery_Google Chrome - Fatal编程技术网

Javascript jQuery('data')返回“data”中包含缺失数据的html片段

Javascript jQuery('data')返回“data”中包含缺失数据的html片段,javascript,jquery,google-chrome,Javascript,Jquery,Google Chrome,我调用jQuery get来获取一个html片段,并基于该数据进行一些处理 $.get($('.more a').attr('href'), function (data) { var $data = $(data); // other code using $data }); 这里的“数据”是 <div class="topDiv"> Data goes here </div> 但美元数据显示: <div class="

我调用jQuery get来获取一个html片段,并基于该数据进行一些处理

$.get($('.more a').attr('href'), function (data) {
        var $data = $(data);
        // other code using $data
    });
这里的“数据”是

<div class="topDiv"> Data goes here </div>
但美元数据显示:

<div class="topDiv"> </div>
只显示html标记,而不显示其中的文本。我也有同样的问题。但是,Firefox可以正确地解析它

当我向$data.html发出警报时,Firefox会用正确的html显示警报,而chrome和IE会用缺少的文本显示警报

由于我在localhost上运行我的项目,我无法提供html片段的URL。我使用asp.net ashx处理程序来提供html片段。基本上,当我们向处理程序发送请求时,它会根据传递给它的查询字符串参数返回一个html片段

String content = "<html> <head> </head> <body>"+
"<div class="topDiv"> Data goes here </div>"+
"</body></html>";

context.Response.ContentType = "text/html";
context.Response.Write(content);
决心

如回答中所述,“html”片段的格式不正确。Firefox3似乎接受了这一点,而Firefox4、Chrome和IE则没有

我怀疑这是因为文本本身并不是有效的html文档。IIRC,div只应包含块级元素。文本应该在p、a等内部。因此IE和Chrome正在根据DOM标准杀死文本,Firefox正在放飞它。告诉jQuery您得到的数据类型是文本,然后使用htmltextData将内容插入到您的div中

例如:

这是如何做到的:

String content = "<html> <head> </head> <body>"+
"<div class="topDiv"> Data goes here </div>"+
"</body></html>";

有并没有用未加修饰的引号编译过?如果您修复了此问题,您应该会感到不安。

如果您向$data.html发出警报,会发生什么情况?能否告诉我们URL是什么,以便我们可以验证它是否将浏览器引导到有效的html文档?为了繁殖呢?
String content = "<html> <head> </head> <body>"+
"<div class="topDiv"> Data goes here </div>"+
"</body></html>";