如何在 CodeIgniter 中包含 jQuery 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8814851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:05:53  来源:igfitidea点击:

How do I include jQuery file in CodeIgniter?

jquerycodeigniter

提问by maan81

I would like to use jQuery inside my CodeIngiter Project. I dont know how to include the js file.

我想在我的 CodeIngiter 项目中使用 jQuery。我不知道如何包含 js 文件。

I would like to have something like below in my view

我想在我看来像下面这样

<script src='<?php echo base_url().APPPATH ?>js/jquery.js'></script>
<script src='<?php echo base_url().APPPATH ?>js/my-js.js'></script>

回答by Rooneyl

Firstly, create a folder to put it in. Then you could use a template system like this one.

首先,创建一个文件夹,把它。然后你可以使用一个模板系统,如这一个

This lets you add js files either globally in the main template, or by a controller/function basis using the syntax $this->template->add_js('assets/js/jquery.js');

这使您可以在主模板中全局添加 js 文件,或者使用语法通过控制器/函数基础添加 js 文件 $this->template->add_js('assets/js/jquery.js');

回答by Trey Davis

Codeigniter has a javascript class: http://codeigniter.com/user_guide/libraries/javascript.html

Codeigniter 有一个 javascript 类:http: //codeigniter.com/user_guide/libraries/javascript.html

You would load the jquery library through:

您将通过以下方式加载 jquery 库:

$this->load->library('jquery');

Then you can paste this into your head section of your view:

然后,您可以将其粘贴到视图的头部部分:

<?php echo $library_src;?>
<?php echo $script_head;?>

For additional javascript files, I typically create a resource folder in my application folder, then use the base_url function to link to the files like:

对于其他 javascript 文件,我通常在我的应用程序文件夹中创建一个资源文件夹,然后使用 base_url 函数链接到如下文件:

<script src="<?php echo base_url('resources/name-of-js-file.js');?>" type="text/javascript"></script>

回答by lazos

The base_url()requires to load the helper 'url'.

base_url()需要加载帮手“URL”。

In order to archive that go to application/config/autoloader.phpand add it on the variable $autoloader['helper']like this:

为了存档转到application/config/autoloader.php并将其添加到变量上,$autoloader['helper']如下所示:

$autoload['helper'] = array('url');

回答by Dau

add a new folder 'js' in your applicationfolder and in your views add this line

在您的application文件夹中添加一个新文件夹“js”,并在您的视图中添加这一行

<script type="text/javascript" src="<?php echo base_url();?>js/name of your js file"></script>

回答by Jakub

You include the JS file like any other HTML output.

您可以像任何其他 HTML 输出一样包含 JS 文件。

Refer to where your JS is located (say its /js from root) and then in your <head></head>portion add the following:

参考您的 JS 所在的位置(从根目录说它的 /js),然后在您的<head></head>部分中添加以下内容:

<script type="text/javascript" src="/js/jquery.min.js"></script>