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



应该很简单,但是我在这个问题上会发疯。基本上,我要做的是在返回的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);
    }
});

如何预处理HTML数据并将所有HREF属性替换为#,然后将OnClick属性添加到相同的链接??

我会使用浏览器的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);

最新更新