Codeigniter:如何包含 javascript 文件 [英] Codeigniter: How to include javascript files
问题描述
您好,我刚刚开始使用 CodeIgniter 框架.我当前的目录结构是
Hello I just started working with CodeIgniter framework. My current directory structure is
Demo(Project name)
+System
+Application
-Controllers
demo.php
+Model
-Views
view_demo.php
-Js
ajax.js
jquery.js
请告诉我如何在 view_demo.php 中包含 .js 文件.
Please tell me how to include .js files in view_demo.php.
谢谢拉杰
推荐答案
您需要使用 base_url()
将 javascript 文件包含在您的 VIEW 中.
You need to use the base_url()
to include the javascript file in your VIEW.
所以,在 view_demo.php 文件中:
So, in the view_demo.php file:
<script type="text/javascript" src="<?=base_url()?>js/jquery.js" ></script>
<script type="text/javascript" src="<?=base_url()?>js/ajax.js" ></script>
您将需要加载 URL 帮助程序.要加载您需要放在 demo.php 控制器上的帮助程序:
You will need the URL helper loaded. To load the helper you need to put on your demo.php controller:
$this->load->helper('url');
您还可以在 helpers 数组上的 configautoload.php 上自动加载.
You can also autoload on configautoload.php on the helpers array.
有关 base_url() 的更多信息:http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_urlhttps://codeigniter.com/user_guide/general/styleguide.html#短开标签
More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url https://codeigniter.com/user_guide/general/styleguide.html#short-open-tags
这篇关于Codeigniter:如何包含 javascript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!