【问题标题】:url_for doing strange things with jqueryurl_for 用 jquery 做奇怪的事情
【发布时间】:2018-09-07 06:12:34
【问题描述】:

我目前正在为 python/flask/Jquery 苦苦挣扎! 我想在img标签中使用URL_for函数,但是在使用的时候,浏览器做出了奇怪的事情!

$("div.left").append("<img id='my_img' class='centered' style='width:100%; height:100%' src=" + "{{ url_for('static', filename='./img/logo.png') }}" + "alt='Loading error!' />")

这是浏览器给我的:

<img id="my_img" class="centered" style="width:100%; height:100%" src="/static/img/logo.pngalt='Loading" error!'="">

最后一个问题:是否可以将代码放在带有 url_for 的 js 文件中(似乎不适合我!)?

【问题讨论】:

  • 我认为问题在于使用单引号和双引号。最好将 {{ url_for('static', filename='./img/logo.png') }} 保存在单独的变量中并连接起来。

标签: javascript python jquery html flask


【解决方案1】:

我认为是双引号和单引号引起的问题。你能试试吗?

$("div.left").append('<img id="my_img" class="centered" style="width:100%; height:100%" src="{{url_for("static", filename="./img/logo.png")}}" alt="Loading error!" />')

【讨论】:

  • 谢谢!它对我有用!你对我的第二个问题有什么想法吗?
【解决方案2】:

如果您正在尝试使用 Jquery,请按照以下代码进行操作

$(function() {
  $("div.left").append("<img id='my_img' class='centered' style='width:100%; height:100%' src='" + url_for('static','./img/logo.png')+"' alt='Loading error!' />")
});

function url_for(a,b){
    return b;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多