如何在 Laravel 5 中使用锚点返回视图?

Tom*_*asz 3 php laravel

在我的控制器中

return view('pages.index', compact('errors'));
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我不知道如何将视图返回到锚点。

return view('pages.index#contact', compact('errors'));
Run Code Online (Sandbox Code Playgroud)

不工作。出现这样的错误。

InvalidArgumentException in FileViewFinder.php line 137:
View [pages.index#contact] not found.
Run Code Online (Sandbox Code Playgroud)

Ale*_*nin 5

你可以使用一些JS。anchor首先传递变量:

$anchor = 'contact';
return view('pages.index', compact('errors', 'anchor'));
Run Code Online (Sandbox Code Playgroud)

然后使用 hidden或其他东西将数据传递给JS:

@if (isset($anchor))
    <input type="hidden" name="anchor" value="{{ $anchor }}">
@endif
Run Code Online (Sandbox Code Playgroud)

最后使用JS移动页面:

$(function () {
    if ( $( "[name='anchor']" ).length ) {
        window.location = '#' + $( "[name='anchor']" ).val();
    }
};
Run Code Online (Sandbox Code Playgroud)

这只是一个给你一个想法的例子。