有没有办法将 render() 与片段参数一起使用,以便在页面加载时自动滚动到页面的特定部分?类似于我们如何调用
redirect(controller: "book", action: "show", fragment: "profile")
最佳答案
您不能将其传递给 render()
,因为在您实际调用 render()
时,URL 已经确定并映射到您的操作;所有渲染正在做的是控制什么被写回响应。
在调用呈现 Controller 操作之前,片段必须已经在 URL 中。这是一个例子:
grails-app/controllers/MyController.groovy
class MyController {
def foo = {
render(view: 'foo')
}
def quux = {
redirect(action: 'foo', fragment: 'baz')
}
}
grails-app/views/my/foo.gsp
<html>
<head>
<title>Foo</title>
</head>
<body>
<a id="bar">Bar</a>
<g:each in="${0..100}"><br/></g:each>
<a id="baz">Baz</a>
</body>
</html>
具有各种 URL:
http://example.com/myapp/my/foo - doesn't scroll to an anchor
http://example.com/myapp/my/foo#baz - scrolls to the 'baz' anchor
http://example.com/myapp/my/quux - scrolls to the 'baz' anchor'
关于带有片段参数的 Grails render(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861738/