JQuery从1.51升级到2后,我收到语法错误

代码只更改了JQuery版本。 ajax调用的简化版本是:

$.ajax( { type: 'GET', url: MapPath($(this).attr('path')), cache: false, data: '{}', dataType: 'html', success: function (result) { console.log(result); if ($(result).filter('#feedback').length > 0) { $('#center').children(':first').before($(result).filter('#feedback').outerHTML()); } else { $('#feedback').remove(); $('#dialog').html(result); $('#dialog').dialog('option', 'title', 'Edit Mailbox'); $('#dialog').dialog('open'); } } }); 

就在if语句之前,我记录结果。 它显示了预期的格式正确的HTML代码段。 片段意味着它包含带有子元素的DIV。 当我在if语句中使用选择器时,我得到错误:

 throw Error("Syntax error, unrecognized expression: 

我可以使用控制台查看结果的内容,如果我尝试任何选择器,我会收到错误。 如果我只是使用,我甚至会得到错误:

 $(result) 

但是,如果我修改结果并添加并附加那么它没有错误。 是什么改变了JQuery来打破这个,我该如何解决它? 结果需要是一个HTML片段,因为我在页面上设置了一个容器值。

由于我无法看到你的html字符串,我无法确切地指出是什么导致它无法使用$() ,但是修复它应该像在result首先使用$.parseHTML()一样简单。 如果没有正确解析它,则html无效。

 result = $.parseHTML(result); console.log(result); if ($(result).filter('#feedback').length > 0) { $('#center').children(':first').before($(result).filter('#feedback').outerHTML()); } else { $('#feedback').remove(); $('#dialog').html(result); $('#dialog').dialog('option', 'title', 'Edit Mailbox'); $('#dialog').dialog('open'); }