- CodeIgniter 教程
- CodeIgniter - 主页
- CodeIgniter - 概述
- CodeIgniter - 安装 CodeIgniter
- CodeIgniter - 应用程序架构
- CodeIgniter - MVC 框架
- CodeIgniter - 基本概念
- CodeIgniter - 配置
- CodeIgniter - 使用数据库
- CodeIgniter - 库
- CodeIgniter - 错误处理
- CodeIgniter - 文件上传
- CodeIgniter - 发送电子邮件
- CodeIgniter - 表单验证
- CodeIgniter - 会话管理
- CodeIgniter - Flashdata
- CodeIgniter - 临时数据
- 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 文件example.js和一个 CSS 文件style.css。现在,要将这些文件添加到您的视图中,请在控制器中加载 URL 帮助程序,如下所示。
$this->load->helper('url');
在控制器中加载 URL 帮助程序后,只需在视图文件中添加以下给定的行,即可在视图中加载 example.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