Python jQuery将图片URL转换为img 标签方法是什么,,后端使用的是Flask,


后端使用的是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>    <div data-role="page" id="pageone">        <div data-role="header" data-fullscreen="true">            <a href="/admin/" data-role="button">首页</a>            <h1>产品详情</h1>        </div>        <div data-role="content">            <div data-role="fieldcontain">                <h2>{{ product.name }}</h2>            </div>            <hr>            <div data-role="fieldcontain">                <div id="desc"></div>            </div>        </div>    </div>    <script>    var txt = {{ product.description}};    $('#desc').text(txt);    </script>   <div data-role="footer" data-fullscreen="true">    <h1>联系方式</h1></div></body></html>

其中,var txt = {{ product.description}};一句,如果不换成var txt = {{ product.description|tojson|safe}};的话就会什么也不输出。

干嘛不直接把{{ product.description}}对应的内容编译进html里,编译进js再操作有点多此一举。。。

编橙之家文章,

评论关闭