我對具有不同設計的不同頁面具有相同的頭文件。我必須在頁眉部分添加所有的CSS文件,這會增加頁面加載。 我的問題是,有沒有辦法將css文件動態加載到標題部分。我正在使用codeigniter框架。在CodeIgniter中動態添加Javascript和CSS文件頭部分
-1
A
回答
0
要笨實現這一點,首先設定在config.php默認的CSS和JavaScript文件
$config['header_css'] = array('style.css','prettyPhoto.css','nivo-slider.css');
$config['header_js'] = array('core.js','core.js',
'jquery-1.4.1.min.js',
'jquery-slidedeck.pack.lite.js',
'jquery-prettyPhoto.js',
'jquery.nivo.slider.js');
然後,添加此輔助功能我寫的,你的模板助手(或者你使用任何其他幫助程序文件)
//Dynamically add Javascript files to header page
if(!function_exists('add_js')){
function add_js($file='')
{
$str = '';
$ci = &get_instance();
$header_js = $ci->config->item('header_js');
if(empty($file)){
return;
}
if(is_array($file)){
if(!is_array($file) && count($file) <= 0){
return;
}
foreach($file AS $item){
$header_js[] = $item;
}
$ci->config->set_item('header_js',$header_js);
}else{
$str = $file;
$header_js[] = $str;
$ci->config->set_item('header_js',$header_js);
}
}
}
//Dynamically add CSS files to header page
if(!function_exists('add_css')){
function add_css($file='')
{
$str = '';
$ci = &get_instance();
$header_css = $ci->config->item('header_css');
if(empty($file)){
return;
}
if(is_array($file)){
if(!is_array($file) && count($file) <= 0){
return;
}
foreach($file AS $item){
$header_css[] = $item;
}
$ci->config->set_item('header_css',$header_css);
}else{
$str = $file;
$header_css[] = $str;
$ci->config->set_item('header_css',$header_css);
}
}
}
if(!function_exists('put_headers')){
function put_headers()
{
$str = '';
$ci = &get_instance();
$header_css = $ci->config->item('header_css');
$header_js = $ci->config->item('header_js');
foreach($header_css AS $item){
$str .= '<link rel="stylesheet" href="'.base_url().'css/'.$item.'" type="text/css" />'."\n";
}
foreach($header_js AS $item){
$str .= '<script type="text/javascript" src="'.base_url().'js/'.$item.'"></script>'."\n";
}
return $str;
}
}
現在,您可以手動添加CSS和JavaScript文件如下:
add_css(array('demo.css','demo1.css'));
add_js('demo.js');
+0
你可以請按照[鏈接](http://jamshidhashimi.com/2013/04/12/dynamically-add-javascript-and-css-files-in-codeigniter-header-page/),我已將表格複製到 –
+0
謝謝歐麥爾汗, 我已經使用它,但它不能正常工作。 –
相關問題
- 1. 使用javascript動態添加javascript和css文件到html
- 2. JS頭部動態添加
- 3. jQuery $(window).load()事件和動態添加外部CSS/JS文件
- 4. 在java web應用程序中動態添加外部JavaScript或CSS文件
- 5. 使用JavaScript動態加載CSS文件
- 6. 在Genshi(TurboGears 2)中添加小部件(動態)CSS
- 7. CSS或JavaScript文件從視圖或局部視圖添加到佈局頭部
- 8. 添加javascript和css文件來反應
- 9. Codeigniter動態CSS
- 10. 在qt中動態添加小部件
- 11. 如何使用外部css文件動態添加樣式?
- 12. 動態加載外部JavaScript文件
- 13. 如何在頭文件中添加動態信息?
- 14. 使用javascript添加動態CSS?
- 15. 添加CSS動態
- 16. RestSharp AddFile將多部分表單頭文件添加到文件
- 17. 無法加載外部JavaScript文件 - Codeigniter
- 18. PHP在佈局模板中添加外部css和js文件
- 19. 在View中動態插入依賴項(Javascript和CSS文件)
- 20. 在碼頭加載動態文件
- 21. 在javascript中加載動態CSS代碼
- 22. 如何在CXF客戶端動態添加HTTP頭文件?
- 23. 添加部分與動態數組
- 24. JavaScript刪除動態添加分區
- 25. 如何在點擊href鏈接後動態添加css頭
- 26. 將動態CSS添加到MVC中的靜態CSS文件內容
- 27. 如何動態地添加CSS來的CKEditor沒有css文件
- 28. PHP模板添加動態js和css
- 29. 在樹枝子中添加css和javascript
- 30. 將CSS添加到JavaScript文件並動態加載到HTML頭標記對性能有效嗎?
什麼d你的意思是動態加載css文件? – kikuyu1
如果你動態添加你的標題視圖,那麼你可以添加變量到標題併發送它像控制器'load-> view('header.php',$ css_js_var)' –
我覺得這個是最好的ide –