如何在Laravel中使用/集成jQuery [英] How to use/integrate jquery in laravel

查看:778
本文介绍了如何在Laravel中使用/集成jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何处理?我的意思是我需要在项目中安装或管理一些文件吗?我不知道该怎么办?抱歉,我是laravel的新人

Any idea how to deal with that? I mean do I need to install or manage some files in my project? I don't know how to manage that? Sorry I am just new in laravel

推荐答案

我将分享两种方式

a)在您的视图中,在html head

a) In your view add this line in html head

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>并开始在script标记中编写您的jquery代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> and start writing your jquery code in script tag

b)我通常更喜欢的另一种方式是:

b) The other way which i generally prefer is :

在Laravel 5.6中,如果您查看您的package.json文件jquery已经作为npm依赖项存在,那么

In Laravel 5.6 if u look at your package.json file jqueryis already present as npm dependency so

  1. 创建新的laravel项目laravel new my-project
  2. cd到my-project并运行命令npm install.这将安装package.json中包括的所有依赖项,包括jquery.
  3. 现在要将所有这些依赖关系编译为一个文件,在public/js/app.js中,将行window.Jquery = require('jquery');添加到您的resources/assets/js/app.js
  4. 运行命令npm run dev

  1. create new laravel project laravel new my-project
  2. cd to my-project and run command npm install. This will install all dependencies included in package.json including jquery.
  3. now to compile down all these dependencies to one file in public/js/app.js add the line window.Jquery = require('jquery');to your resources/assets/js/app.js
  4. run command npm run dev

将行<script src="{{ asset('js/app.js') }}"></script>添加到您的welcome.blade.php

现在将此代码添加到您的视图中.

add this code to your view now.

<script>
    $(document).ready(function(){
    $(".links").hide();
});

刷新页面.您现在将不会看到laravel下面的链接.这意味着jQuery已启动并正常工作

refresh page. you will not see the links below laravel now. which means jquery is up and working

这篇关于如何在Laravel中使用/集成jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆