CodeIgniter和Javascript / Jquery库
正如标题所说,我正在试图弄清楚如何在CI上使用javascript和jquery库。
按照文档中的说明 ,我在我的控制器中加载库:
$this->load->library('javascript');
然后,我在config.php中定义jQuery文件(jquery.min.js)的位置:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
之后,我打开视图文件并输入以下两行:
第一个错误出现在这里: 未定义的变量$ library_src和$ script_head (不明白我必须设置它们的位置)
无论如何,我已经评论了这些行并继续使用jquery lib,通过在我的控制器中加载它:
$this->load->library('jquery');
下一个错误: 无法加载请求的类:jquery 。 (似乎它无法找到lib,我搞砸了什么?)
检查系统文件夹,它看起来所有文件都已到位:
system/libraries/Javascript.php system/libraries/javascript/Jquery.php
在此先感谢您的帮助!
重要的是要注意这个驱动程序被标记为实验,所以我不会依赖它。
此外,我个人认为这是一个混乱和令人头疼的问题,试图直接将应用程序的服务器端部分与客户端部分混合在一起。
要在您的视图中使用javascript,我最初会像这样加载它们……
将代码放在config.php中,如下所示:
$config['javascript_location'] = 'js/jquery/jquery.js'; $config['javascript_ajax_img'] = 'images/ajax-loader.gif';
在您的控制器文件(例如controllers / sample.php)中键入以下代码:
function __construct() { parent::__construct(); $this->load->library('javascript'); } function index() { $data['library_src'] = $this->jquery->script(); $data['script_head'] = $this->jquery->_compile(); $this->load->view('sampleview', $data); }
在您的视图文件(例如views / sampleview.php)中输入以下代码:
这对我有用。 我希望它对你也有用。 XD
因为这个驱动程序是实验性的,所以文档还不是很完整。 但我能够找到解决方案。
首先,文档中有错误。 除非您更改核心Javascript库(未建议),否则引用变量不是 $script_head
而是$script_foot
。
其次,一旦你完成了通话,你似乎需要运行
$this->javascript->external();
和
$this->javascript->compile();
这些函数设置$library_src
和$script_foot
变量。
把它们放在一起,在你的控制器中,你将拥有:
class Some_Controller extends CI_Controller { public function index() { $this->javascript->click('#button', "alert('Hello!');"); $this->javascript->external(); $this->javascript->compile(); $this->load->view('index'); } }
在你看来你会有
虽然CI首先查找system
文件夹,但您也可以尝试将lib放在这些文件夹中:
application/libraries/Javascript.php application/libraries/javascript/Jquery.php
不确定为什么你必须通过控制器加载你的js文件,然后在你的视图中回显它。 您可以直接在视图中使用HTML标记加载ur js文件。 当变量的值是动态/需要从数据库等加载时,主要使用从控制器传递数据并在视图中回显它。
正如我们在用户指南中所知道的那样,首先,它是一个实验性但有效的。 第一步是打开application / config / config.php下的配置文件。
放下以下行:
// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js' $config['javascript_location'] = 'js/yourfile.js';
第二步是打开你的控制器,在构造函数方法中,输入以下代码:
// Load javascript class $this->load->library('javascript'); $this->load->library('javascript/jquery'); // if u want to use jquery
然后,仍然在索引方法中的控制器文件中:
$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().
然后打开您的视图文件并将以下代码放在标记头中:
那种方式对我有用,让我们来试试吧。
使用:
$this->load->library('javascript/jquery');
代替:
$this->load->library('jquery');
这将加载您的jQuery库。
我遇到了同样的问题,发现:
load->library('javascript'); ?> load->library('javascript/jquery'); ?>
在https://ellislab.com/forums/viewthread/181742/#860506上
因为jquery在javascript文件夹中。
要么
$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');
在autoload.php文件中。
这解决了加载librairy的问题。
你可以试试这个,这对我有用。
在config.php中
$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
在控制器中
public function __construct() { parent::__construct(); $this->load->library('javascript'); $this->load->library('javascript/jquery'); } public function index(){ $d['library_src'] = $this->jquery->script(); $d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');"); $this->load->view('main',$d); }
在视野中(头部)
在视图内容(正文)
在javascript视图中
快乐的编码:)