Jquery - 无法从AJAx响应中删除()

时间:2015-10-13 15:32:19

标签: javascript jquery html ajax

我目前正在尝试从<link ..>获取的xhtml文件中删除CSS-Tags <script ...>和JS-Tags $.get() 但正如您在附件firebug output screenshot中看到的那样,它似乎不起作用。尽管temp.filter('link').remove()应该得到正确的标签,但temp变量似乎没有变化。我的代码不起作用,还是我不是以正确的方式解释萤火虫输出?

我尝试按照此处的说明实现它:https://stackoverflow.com/a/26319507/5400948

我的代码如下:

$.get("../BaseApplication/user/userList.xhtml", function(data) {
    var temp = $(data);
    console.log(temp);
    console.log(temp.filter('link').remove());
    console.log(temp);
});

firebug output screenshot

2 个答案:

答案 0 :(得分:1)

发布这个,因为我找到了一个适合我的解决方案:

$.get("../BaseApplication/user/userList.xhtml", function(data) {
  var temp = $(data);
  temp = temp.not('link').not('script'));
  $('#mainContent').html(temp);
});

这会从AJAX响应中删除所有<link ...><script ...>标记,并替换<div id="mainConent">

内的所有内容

答案 1 :(得分:0)

我之前的回答不正确。我看过,找不到解释原因的文档。但是,这将有效:

var temp = $('<div>').append($(test)).find('link').remove().end().unwrap();
$('body').append(temp);

基本上,这会将html附加到div,在div中搜索链接元素,删除它们,然后向下移动到div元素,从剩余的html中删除div包装器,然后附加修改后的结果。

老实说,我无法找到解释这种行为的任何东西,但事实确实如此。我希望它有所帮助。