CodeIgniter 和 Javascript/Jquery 库
正如标题所说,我试图弄清楚如何在 CI 上使用 javascript 和 jquery 库。
按照文档中的说明,我将库加载到控制器中:
$this->load->library('javascript');
然后,我定义 jQuery 文件的位置(jquery.min.js) 在 config.php 中:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
之后,我打开视图文件并放入这两行:
<?php echo $library_src;?>
<?php echo $script_head;?>
这里出现第一个错误:未定义的变量 $library_src 和 $script_head (don不明白我必须在哪里设置它们)
无论如何,我已经评论了这些行并继续使用 jquery lib,通过将其加载到我的控制器中:
$this->load->library('jquery');
下一个错误:无法加载请求的类:jquery 。 (似乎找不到该库,我搞砸了什么?)
检查系统文件夹,看起来所有文件都已就位:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
提前感谢您的帮助!
As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.
Following instruction in the docs, I load the library in my controller:
$this->load->library('javascript');
Then, I define the location of the jQuery file (jquery.min.js) in the config.php:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
After that, I open the view file and put in these two lines:
<?php echo $library_src;?>
<?php echo $script_head;?>
First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)
Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:
$this->load->library('jquery');
Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)
Checking on system folder it looks all files are in place:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将代码放入 config.php 中,如下所示:
在您的控制器文件(例如,controllers/sample.php)中,键入以下代码:
在您的视图文件(例如,views/sampleview.php)中,键入以下代码:
这对我有用。我希望它也适合你。 XD
Put the code in the config.php like this:
In your controller file (e.g. controllers/sample.php) type this codes:
In your view file (e.g. views/sampleview.php) type this codes:
This works for me. I hope it works for you too. XD
请务必注意,此驱动程序被标记为实验,因此我不会依赖它。
另外,我个人认为尝试直接将应用程序的服务器端部分与客户端部分混合会带来混乱和头痛。
要在你的视图中使用 javascript,我首先要像这样加载它们......
It is important to note that this Driver is marked as experimental so I wouldn't rely on it.
Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.
To use javascript in your views, I would just start out by loading them like this...
因为这个驱动程序是实验性的,所以文档还不完整。但我能够找到解决方案。
首先,文档中有错误。除非您更改核心 Javascript 库(不建议),否则引用变量不是
$script_head
,而是$script_foot
。其次,一旦完成调用,似乎您需要运行
这些
函数设置
$library_src
和$script_foot
变量。要将所有这些放在一起,在您的控制器中您将拥有:
在您看来,您将拥有
Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.
First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not
$script_head
but in fact$script_foot
.Second, once you've finished making your calls, it seems you need to run
and
These functions set the
$library_src
and$script_foot
variables.To put it all together, in your controller you would have:
In your view you would have
虽然 CI 首先查找
system
文件夹,但您也可以尝试将库放入这些文件夹中:Although CI first looks for
system
folder, you can also try putting your libs in the these folders:不知道为什么你必须通过控制器加载 js 文件,然后在你的视图中回显它。您可以直接在视图中使用 HTML 标签加载您的 js 文件。当变量的值是动态的/需要从数据库等加载时,主要使用从控制器传递数据并在视图中回显它。
Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.
正如我们在用户指南中所知,首先,它是一个实验性的但有效的。第一步是打开 application/config/config.php 下的配置文件。
放置以下行:
第二步是打开控制器,在构造函数方法中,放置以下代码:
然后,仍在索引方法中的控制器文件中:
然后打开视图文件并将以下代码放置在标记头中:
这种方式适用于我,我们试试吧。
As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .
Put the following line :
Second step is open your controller, within constructor method, put the following code :
Then, still in controller file within index method :
Then open your view file and put the following code within tag head :
That way is working for me, let's try it.
使用:
而不是:
这将加载您的 jQuery 库。
Use:
instead of:
This will load your jQuery library.
我遇到了同样的问题,并发现:
在 https://ellislab.com/forums/viewthread/ 181742/#860506
因为 jquery 位于 javascript 文件夹中。
或者
在 autoload.php 文件中。
这样就解决了加载库的问题。
I had the same problem, and found :
on https://ellislab.com/forums/viewthread/181742/#860506
Because jquery is in javascript folder.
Or
In the autoload.php file.
That solved the problem of loading the librairy.
你可以试试这个,这对我来说很有用。
在config.php中
在控制器中
在视图(头)
中在视图内容(正文)
中在javascript中查看
快乐编码:)
You can try this, it's work to me.
in config.php
in Controller
in view (head)
in view content (body)
in javascript view
happy coding :)