简体   繁体   English

查找并替换为ajax调用?

[英]Find and replace in an ajax call?

I have an ajax method that returns HTML. 我有一个返回HTML的ajax方法。

In that returned HTML I wish to replace certain divs with data. 在返回的HTML中,我希望用数据替换某些div。

I've looked at replaceWith but this seems to operate on dom objects only, not on HTML returned from an ajax call. 我已经看过replaceWith,但这似乎只对dom对象起作用,而不对从ajax调用返回的HTML起作用。

How can I replace certain divs inside the success method in an ajax call? 如何在ajax调用中替换成功方法内的某些div?

$.ajax({
    url: '/get-data',
    dataType: 'html',
    success: function(html) {
        //find div with class .test and replace it with <div class"abc"></div>

You can use replaceWith() , like this: 您可以使用replaceWith() ,如下所示:

$.ajax({
    url: '/get-data',
    dataType: 'html',
    success: function(html) {
        var $html = $(html);
        $html.find('.test').replaceWith('<div class="abc"></div>');

        // work with $html as required here...
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM