【问题标题】:Codeigniter and jQuery css switcherCodeigniter 和 jQuery CSS 切换器
【发布时间】:2011-01-08 20:13:36
【问题描述】:

我一直在关注tutorial 使用 PHP 和 jQuery 创建样式切换器,现在在教程中 PHP 函数使用获取数据,这在 codeigniter 中不可用,我希望有人能够帮助我整理对不起尝试?

我的 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();

}

单击链接以更改样式表时,我通过 firebug 得到此错误,

遇到错误

您提交的 URI 包含不允许的字符。

正在发送的网址是,

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

这个例子是我点击绿色选项。

【问题讨论】:

    标签: php css codeigniter jquery-plugins jquery


    【解决方案1】:

    您没有在 jquery 中调用函数 setBackground()。

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

    您需要在标签中添加 id red 和 blue。 你需要这样称呼它。

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

    【讨论】:

      【解决方案2】:

      尝试从以下位置删除 +'&js' 部分:

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

      在 jQuery 插件中。

      【讨论】:

      • 谢谢,解决了我的错误,但是数据变量是空的,你知道为什么会这样吗?
      • 查看 firebug 中的 net/xhr 选项卡,查看调用 url 后返回的内容。也许你得到了错误的部分。我会将 funcion 更改为“function setBackground($style) {”并删除按段获取 $style。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多