[英]how to use jquery in a laravel child blade file
您好,我是 laravel 的新用戶 7. 我想在刀片文件中使用 jquery。 如果我創建一個刀片文件並在其頂部提到所有頭部和身體,然后在其中提供 jquery 的鏈接,那么它就可以正常工作。 但在我的例子中,我創建了一個 nav.blade.php 文件,我在其中放置了所有導航頭和主體代碼。 然后我創建另一個文件並在該文件的頂部擴展導航文件,但在這種情況下 jquery 不起作用
nav.blade.php代碼
<html>
<head>
<meta charset="utf-8">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<!-- the below i use for jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{{'addcategory'}}">Add Catgory <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{'showcategory'}}">show Category</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Manage Products
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{'
addproducts'}}">Add Products</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
@yield('content')
</body>
</html>
showcategories.blade.php 文件這是我要使用 jquery 的文件,上面的文件在這個文件里面擴展
@extends('layouts.nav')
@section('content')
<table class="table">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">name</th>
<th scope="col">Delete</th>
<th scope="col">Update</th>
<th scope="col">Get Products</th>
</tr>
</thead>
<tbody>
@foreach($categories as $cat)
<tr>
<td>{{$cat->id}}</td>
<td>{{$cat->name}}</td>
<td><a class="btn btn-danger" href="{{url('delete',$cat->id)}}">Delete</a> </td>
<td><a class="btn btn-danger" href="{{url('update',$cat->id)}}">update</a> </td>
<td><a class="btn btn-danger" href="{{url('getproductjoin',$cat->id)}}">getproducts</a> </td>
</tr>
@endforeach
</tbody>
</table>
<button id="hello" class="btn btn-danger">hello</button>
@endsection
@section('script')
<script >
$(document).ready(function() {
$("#hello").click(function () {
alert("hello there");
})
});
</script>
@endsection
在此文件上,jquery 不起作用。 請幫我
您需要遵循 Laravel 語法和標准來執行此操作。 您可以為應用程序創建標准布局文件。 即main.blade.php
,在該文件中,您可以根據需要設置@yield
所需的內容。 例如你的 main.blade.php 可以是這樣的:
<html>
<head>
<meta charset="utf-8">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<!-- the below i use for jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{{'addcategory'}}">Add Catgory <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{'showcategory'}}">show Category</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Manage Products
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{'
addproducts'}}">Add Products</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
@yield('content')
</body>
@yield('scripts')
</html>
你的 nav.blade.php 看起來像這樣。
@extends('layouts.main')
@section('content')
<table class="table">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">name</th>
<th scope="col">Delete</th>
<th scope="col">Update</th>
<th scope="col">Get Products</th>
</tr>
</thead>
<tbody>
@foreach($categories as $cat)
<tr>
<td>{{$cat->id}}</td>
<td>{{$cat->name}}</td>
<td><a class="btn btn-danger" href="{{url('delete',$cat->id)}}">Delete</a> </td>
<td><a class="btn btn-danger" href="{{url('update',$cat->id)}}">update</a> </td>
<td><a class="btn btn-danger" href="{{url('getproductjoin',$cat->id)}}">getproducts</a> </td>
</tr>
@endforeach
</tbody>
</table>
<button id="hello" class="btn btn-danger">hello</button>
@endsection
@section('scripts')
<script >
$(document).ready(function() {
$("#hello").click(function () {
alert("hello there");
})
});
</script>
@endsection
或者您可以遵循組件標准,並可以為您的腳本創建一個組件,以將其包含在您喜歡的任何地方。 您可以在此處閱讀有關 Laravel 鏈接的更多信息: https://laravel.com/docs/7.x/blade#components
你只需要 yield 內容,你還需要 yield 腳本
</nav>
@yield('content')
</body>
@yield('script')
</html>
將其添加到代碼的底部
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.