【发布时间】:2016-03-07 12:23:04
【问题描述】:
在 HTML 中,我可以直接链接到页面上的特定位置,假设存在具有给定 id 的元素:
<a href="http://www.example.com/stuff.html#exactlocation">Go there</a>
在 Flask 中,我尝试将锚点添加到 render_template,但我得到了 jinja2.exceptions.TemplateNotFound: stuff.html#exactlocation。
@main.route('/exactlocation')
def exactlocation():
return render_template('stuff.html#exactlocation')
如何链接到模板中的特定位置?
【问题讨论】:
-
这是浏览器的行为。只需正常渲染您的模板,浏览器就会处理它(假设您在页面上有命名的锚点)。
-
@dirn 我不确定你的意思。如果我使用 return render_template('stuff.html') 浏览器如何知道转到页面上的特定位置而不是顶部,在页面加载时运行 javascript 检查?
-
浏览器会自动跳转到命名锚点,不需要 JavaScript。唯一的要求是命名的锚点(即
<a name="exactlocation"></a>)存在于 HTML 中。