首页 > 解决方案 > 如何使用 Laravel Route 添加 Javascript onClick 函数

问题描述

我添加了一个带有 Javascript onClick 函数的链接,如下所示:

我还需要添加一个 Laravel 的路由,所以我添加了这个:

<div onclick="location.href='{{ route(\'shopping-products.show\', $newest->prd_slug) }}';" style="cursor: pointer;">

但这会返回此错误:

syntax error, unexpected ''shopping-products.show\', $ne' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) (View: index.blade.php)

那么如何正确添加带有 Javascript 的 onClick 功能的 Laravel 路由呢?

标签: javascriptphplaravelonclicklaravel-5.8

解决方案


不需要反斜杠

<div onclick="location.href='{{ route('shopping-products.show', $newest->prd_slug) }}';" style="cursor: pointer;">

推荐阅读