2010-01-24 146 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, ... 
...