jQuery替换ajax加载内容中的字符串
我试图用另一个字符串替换一个字符串的所有实例。以下是我到目前为止的情况: 我通过ajax加载模板html文件,内容如下: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>
<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
它只适用于单引号。我尝试不使用引号和双引号,但不起作用:
namevar 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"));
});