后端使用的是Flask,前端是jQuery Mobile。后端会传一段字符串给前端,这段字符串中会有类似于“http://xxx.jpg”这样的图片URL。现在想要将这个URL直接替换成<img src="http:///xxx.jpg">这样的标签,然后在页面上输出。试着在后端进行正则表达式的替换,结果会出现错误,我的代码如下:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<p data-role="page" id="pageone">
<p data-role="header" data-fullscreen="true">
<a href="/admin/" data-role="button">首页</a>
<h1>产品详情</h1>
</p>
<p data-role="content">
<p data-role="fieldcontain">
<h2>{{ product.name }}</h2>
</p>
<hr>
<p data-role="fieldcontain">
<p id="desc"></p>
</p>
</p>
</p>
<script>
var txt = {{ product.description}};
$('#desc').text(txt);
</script>
<p data-role="footer" data-fullscreen="true">
<h1>联系方式</h1>
</p>
</body>
</html>
其中,var txt = {{ product.description}};一句,如果不换成var txt = {{ product.description|tojson|safe}};的话就会什么也不输出。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
干嘛不直接把{{ product.description}}对应的内容编译进html里,编译进js再操作有点多此一举。。。