- CodeIgniter 教程
- CodeIgniter - 首页
- CodeIgniter - 概述
- CodeIgniter - 安装 CodeIgniter
- CodeIgniter - 应用架构
- CodeIgniter - MVC 框架
- CodeIgniter - 基本概念
- CodeIgniter - 配置
- CodeIgniter - 数据库操作
- CodeIgniter - 库
- CodeIgniter - 错误处理
- CodeIgniter - 文件上传
- CodeIgniter - 发送邮件
- CodeIgniter - 表单验证
- CodeIgniter - 会话管理
- CodeIgniter - Flash 数据
- CodeIgniter - Temp 数据
- CodeIgniter - Cookie 管理
- CodeIgniter - 常用函数
- CodeIgniter - 页面缓存
- CodeIgniter - 页面重定向
- CodeIgniter - 应用性能分析
- CodeIgniter - 基准测试
- CodeIgniter - 添加JS和CSS
- CodeIgniter - 国际化
- CodeIgniter - 安全性
- CodeIgniter 有用资源
- CodeIgniter - 快速指南
- CodeIgniter - 有用资源
- CodeIgniter - 讨论
CodeIgniter - 添加JS和CSS
在 CodeIgniter 中添加 JavaScript 和 CSS(层叠样式表)文件非常简单。您需要在根目录下创建 JS 和 CSS 文件夹,并将所有 .js 文件复制到 JS 文件夹,并将 .css 文件复制到 CSS 文件夹,如图所示。
例如,假设您创建了一个 JavaScript 文件 **sample.js** 和一个 CSS 文件 **style.css**。现在,要将这些文件添加到您的视图中,请在您的控制器中加载 URL 辅助函数,如下所示。
$this->load->helper('url');
在控制器中加载 URL 辅助函数后,只需在视图文件中添加以下几行代码,即可在视图中加载 sample.js 和 style.css 文件,如下所示。
<link rel = "stylesheet" type = "text/css" href = "<?php echo base_url(); ?>css/style.css"> <script type = 'text/javascript' src = "<?php echo base_url(); ?>js/sample.js"></script>
示例
创建一个名为 **Test.php** 的控制器,并将其保存到 **application/controller/Test.php**
<?php
class Test extends CI_Controller {
public function index() {
$this->load->helper('url');
$this->load->view('test');
}
}
?>
创建一个名为 **test.php** 的视图文件,并将其保存到 **application/views/test.php**
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter View Example</title>
<link rel = "stylesheet" type = "text/css"
href = "<?php echo base_url(); ?>css/style.css">
<script type = 'text/javascript' src = "<?php echo base_url();
?>js/sample.js"></script>
</head>
<body>
<a href = 'javascript:test()'>Click Here</a> to execute the javascript function.
</body>
</html>
创建一个名为 **style.css** 的 CSS 文件,并将其保存到 **css/style.css**
body {
background:#000;
color:#FFF;
}
创建一个名为 **sample.js** 的 JS 文件,并将其保存到 **js/sample.js**
function test() {
alert('test');
}
修改 **application/config/routes.php** 中的 **routes.php** 文件,为上述控制器添加路由,并在文件末尾添加以下行。
$route['profiler'] = "Profiler_controller"; $route['profiler/disable'] = "Profiler_controller/disable"
在浏览器中使用以下 URL 执行上述示例。
http://yoursite.com/index.php/test
广告