使用jQuery更改AJAX响应 [英] Using jQuery to alter an AJAX response

查看:95
本文介绍了使用jQuery更改AJAX响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当AJAX调用返回html时,是否可以使用jQuery更改响应字符串的内容?如果是这样,那该怎么做呢?

When an AJAX call returns html, is it possible to use jQuery to alter the contents of the response string? If so, how does one go about doing that?

此问题旨在在将响应 写入页面之前对其进行编辑

This question is directed at editing the response before writing it to the page

推荐答案

取决于您如何请求数据.如果您有$ .ajax等,则可以在成功处理程序中进行必要的修改.我假设由于您不太了解jQuery,因此您正在使用$ .load()加载数据,在这种情况下,最容易用

Well that depends on how you request the data. If you have $.ajax etc, then you can do the necessary modifications in the success handler. I assume since you don't understand jQuery very well, that you are using $.load() to load the data, in which case it's easiest to replace it with

$.get('/somecode.php', function(data) {
    data = $(data);

    // Find and remove all anchor tags for example
    data.find('a').remove();
    $('#target-id').append(data);
});

或者,如果您不想创建jQuery对象,则可以轻松地执行

Or if you don't want to create a jQuery object, you can easily do something like

$.get('/somecode.php', function(data) {
    // Replace all strings "foo" with "bar" for example
    data = data.replace('foo', 'bar');

    // Manually append it to DOM or do whatever you want with it
    $('#target-id').append(data);
});

这篇关于使用jQuery更改AJAX响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆