Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery替换ajax加载内容中的字符串_Jquery_Replace - Fatal编程技术网

jQuery替换ajax加载内容中的字符串

jQuery替换ajax加载内容中的字符串,jquery,replace,Jquery,Replace,我试图用另一个字符串替换一个字符串的所有实例。以下是我到目前为止的情况: 我通过ajax加载模板html文件,内容如下: <div style="background-image:url({URL}/photo.png)"> <div> <p> <h1> <a href="{URL}">link 1</a> </h1>

我试图用另一个字符串替换一个字符串的所有实例。以下是我到目前为止的情况:

我通过ajax加载模板html文件,内容如下:

<div style="background-image:url({URL}/photo.png)">
    <div>
        <p>
           <h1>
             <a href="{URL}">link 1</a>
           </h1>
        </p>
        <a href="{URL}">link 2</a>
    </div>
</div>
或:


但是它们都不起作用。

使用全局替换的正确语法不是
data.replace(“/{URL}/g”)http://example.com");
正确的语法是:
data.replace(/{URL}/g,“http://example.com");

我尝试了下面的代码,它按预期工作:


$().ready(函数(){
$.get(“template.html”,)
.完成(功能(数据){
data=data.replace(/{URL}/g,“http://yourdomain.com");
$(“#content”).html(数据);
});
});

我正在使用python3.5/django2.0

它只适用于单引号。我尝试不使用引号和双引号,但不起作用:


name
var url='{%url“api_调用”“str_to_replace”%}';$。ajax({url:url.replace('str_to_replace',“new string”),

您是否检查了
数据
对象是否返回
模板.html
内容?有时您在这些请求中会遇到C.O.R.S问题。我将您的内容传递给
数据.replace(/{url}/g,“http://example.com”;
我得到了想要的结果。
$.get("template.html")
    .done(function(data) {
        data.replace("/{URL}/g", "http://example.com");
});
$.get("template.html")
    .done(function(data) {
        $(data).html($(data).html().replace("{URL}", "http://example.com"));
});