2010-01-24 64 views
1

我一直在關注使用PHP和jQuery創建樣式切換器的tutorial,現在在教程中PHP函數使用獲取數據,這在codeigniter中不可用,我希望有人能夠幫助我整理我的對不起的嘗試?Codeigniter和jQuery css切換器

我的PHP函數

function setBackground() { 
    $style = $this->uri->segment(3); 
    setcookie("style", $style, time() + (60*60*24*30)); 
    echo $style; 
} 

我的HTML和jQuery呼叫

<ul id="options"> 
    <li><a class="option" href="<?php echo base_url();?>welcome/setBackground/red">Red</a></li> 
    <li><a class="option" href="<?php echo base_url();?>welcome/setBackground/green">Green</a></li> 
</ul> 
<script type="text/javascript"> 
$('a.option').styleSwitcher(); // calling the plugin 
</script> 

的jQuery插件

jQuery.fn.styleSwitcher = function(){ 
$(this).click(function(){ 
    loadStyleSheet(this); 
    return false; 
}); 
function loadStyleSheet(obj) { 
    $('body').append('<div id="overlay" />'); 
    $('body').css({height:'100%'}); 
    $('#overlay') 
     .css({ 
      display: 'none', 
      position: 'absolute', 
      top:0, 
      left: 0, 
      width: '100%', 
      height: '100%', 
      zIndex: 1000, 
      background: 'black url(img/loading.gif) no-repeat center' 
     }) 
     .fadeIn(500,function(){ 
      $.get(obj.href+'&js',function(data){ 
      $('#stylesheet').attr('href','/media/css/' + data + '.css'); 

       cssDummy.check(function(){ 
        $('#overlay').fadeOut(500,function(){ 
         $(this).remove(); 
        }); 
       }); 
      }); 
     }); 
} 
var cssDummy = { 
    init: function(){ 
     $('<div id="dummy-element" style="display:none" />').appendTo('body'); 
    }, 
    check: function(callback) { 
     if ($('#dummy-element').width()==2) callback(); 
     else setTimeout(function(){cssDummy.check(callback)}, 200); 
    } 
} 
cssDummy.init(); 

}

在點擊鏈接以改變樣式表我得到這個e RROR通過螢火蟲rturned,

一個錯誤時遇到

您提交已禁用的字符的URI。

正在發送的是URL,

http://mywebsite/index.php/welcome/setBackground/green&js

這個例子是我點擊鏈接的選擇。

回答

0

嘗試從去除+」 & JS'部分:

... 
$.get(obj.href+'&js',function(data){ 
... 
在jQuery插件

+0

謝謝你,這擺脫了我的錯誤,但數據變量是空的,你知道這是爲什麼嗎? – Udders 2010-01-24 18:14:47

+0

在firebug中查看net/xhr標籤,查看通過調用url返回的內容。也許你得到了錯誤的細分市場。我會改變功能「功能setBackground($風格){」和刪除獲得段風格$風格。 – parserr 2010-01-24 18:57:08

1

你不是在jQuery中調用函數setBackground()。

.fadeIn(500,function(){ 
     $.get(obj.href+'&js',function(data){ 

您需要在標記中添加id紅色和藍色。 你需要稱之爲這樣的東西。

.fadeIn(500,function(){ 
var color=$('.color').attr('id'); 
$.post('yourphpclass/setBackground/'+color, ... 
...