在显示之前替换jQuery ajax调用中的返回数据

时间:2021-02-26 20:14:53

It should be pretty simple, but i'm going crazy on this issue. Basically what i'm trying to do is to replace some tags from returned HTML data before display it on the page. For example:

它应该很简单,但我在这个问题上发疯了。基本上我要做的是在页面上显示之前从返回的HTML数据中替换一些标签。例如:

$.ajax({
    url: url,
    cache: false,
    type: 'GET',
    success: function(data) {
        // Here i need to replace href in ALL <a> tags and add the onclick attrib with the contents from the href 
        $('#floaty-dialog').html(modifieddata);
    }
});

how can i pre-process the HTML data and replace all href attributes with # and add a onclick attrib to that same links??

我如何预处理HTML数据并用#替换所有href属性并将onclick属性添加到相同的链接?

1 个解决方案

#1


0  

I would use the browser's HTML parser to do the work for me:

我会使用浏览器的HTML解析器为我做的工作:

var dom = $(data);
dom.find("a").each(function() {
  var href = this.href;
  $(this).click(function(e) {
    e.preventDefault();
    alert(href);
  });
}).attr("href", "#");

$('#floaty-dialog').html(dom);

#1


0  

I would use the browser's HTML parser to do the work for me:

我会使用浏览器的HTML解析器为我做的工作:

var dom = $(data);
dom.find("a").each(function() {
  var href = this.href;
  $(this).click(function(e) {
    e.preventDefault();
    alert(href);
  });
}).attr("href", "#");

$('#floaty-dialog').html(dom);