如何使用 render() Yii 将锚点发送到 URL

Ily*_* Vo 3 html anchor yii

嗨,我不会用锚点位置渲染视图 - http://example.com/index.php#top

可能是这样的:

$this->render('index', ['data' => $data], 'anchor'=>'top');
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

dar*_*eir 5

当你调用renderurl 时已经设置好了,因为你在controller(或view)中。所以此时你不能在url中添加锚点。

您必须在调用控制器之前设置锚点。为此,您可以在创建 url 时设置一个锚点:

$this->createUrl('controller/action', array('#' => 'the-anchor'));
Run Code Online (Sandbox Code Playgroud)

您还可以使用redirect锚将访问者重定向到页面

$this->redirect(array('controller/action', array('#' => 'the-anchor')));
Run Code Online (Sandbox Code Playgroud)

但我认为在创建 url 时设置锚点是实现您想要的最佳方式。