| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 929 人关注过本帖, 1 人收藏
标题:jquery写的时候挺舒服,过几个月再读的时候就头大了
只看楼主 加入收藏
静夜思
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:济南的冬天
等 级:管理员
威 望:11
帖 子:8902
专家分:2567
注 册:2004-3-25
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:4 
jquery写的时候挺舒服,过几个月再读的时候就头大了
感慨一下
程序代码:
$(document).ready(function(){
    $('div.Tree img').filter(function(){
        return $(this).attr('src').has_any(['plus', 'minus']);
    }).css({cursor:'pointer'}).click(plus_or_minus_click);
    $("div.Tree img").filter(function(){
        return $(this).attr('src').has_any('folder');
    }).css({cursor:'pointer'}).click(function(){$(this).prev('img').click();});

    $favorite_fids = [];
    $('div.favoriteTree a[href*="forumdisplay.php?fid="]').each(function(){
        var $this = $(this);
        if($this.parent().attr('class') == 'depth1'){
            $this.after('<a class="add" href="###" title="从“我感兴趣的论坛”中删除">删除</a>');
            $favorite_fids.push($this.attr('href').match(/fid=(\d+)$/)[1]);
        }
    });
    $('div.favoriteTree a:contains("删除")').click(del_click);

    $('div.Tree a[href*="forumdisplay.php?fid="]').filter(function(){
        return !$(this).parent().hasClass('depth0') && !(($(this).attr('href').match(/fid=(\d+)$/))[1].is_in($favorite_fids));
    }).after('<a class="add" href="###" title="添加到“我感兴趣的论坛”中">添加</a>');
    $('div.Tree a:contains("添加")').click(add_click);

    function plus_or_minus_click(){
        var $this = $(this);
        if($this.attr('src').has('plus')){
            $this.attr('src', $this.attr('src').replace('plus', 'minus'));
            $this.next('img').attr('src', $this.next('img').attr('src').replace('folder', 'folderopen'));
            $this.parent('div').next('div').slideDown(200);
        }else{
            $this.attr('src', $this.attr('src').replace('minus', 'plus'));
            $this.next('img').attr('src', $this.next('img').attr('src').replace('folderopen', 'folder'));
            $this.parent('div').next('div').slideUp(200);
        }
    }

    function add_click(){
        var $this = $(this);
        var $parent = $this.parent();
        var $fid = $this.prev('a').attr('href').match(/fid=(\d+)$/)[1];
        $this.remove();
        $.ajax({
            url: '/frame/left_frame_content.php',
            cache: false,
            type: 'POST',
            data: {action: 'add', 'fid': $fid},
            success: function(data){
                if(data=='ok'){
                    alert("已添加至“我感兴趣的论坛”");
                    var $forumclone = $parent.clone(true);
                    if($forumclone.attr('class')=='depth2'){
                        $forumclone.attr('class','depth1').find('img:nth-child(2)').remove();
                    }
                    var $bottomimg = $forumclone.find('img[src*="middle"]');
                    if($bottomimg.length > 0){
                        $bottomimg.attr('src', $bottomimg.attr('src').replace('middle', 'bottom'));
                    }
                    var $prev_bottomimg = $('.favoriteTree').children('.depth1').last().find('img[src*="bottom"]');
                    $prev_bottomimg.attr('src', $prev_bottomimg.attr('src').replace('bottom', 'middle'));
                    var $emptyimg = $prev_bottomimg.parent().next().find('img[src*="empty"]');
                    if($emptyimg.length > 0){
                        $emptyimg.attr('src', $emptyimg.attr('src').replace('tree_empty', 'tree_line'));
                    }
                    $forumclone.append('<a class="add" href="###" title="从“我感兴趣的论坛”中删除">删除</a>');
                    $forumclone.find('a:contains("删除")').click(del_click);
                    $forumclone.hide();
                    $('.favoriteTree').append($forumclone);
                    $forumclone.show(200);

                    if($parent.find('img[src*="folder"]').length > 0){  //如果含有子版面
                        var $childrenclone = $parent.next().clone(true);
                        $('.favoriteTree').append($childrenclone);
                    }

                    $('div.Tree a[href*="fid={0}"]'.format($fid)).next('a:contains("添加")').remove();
                }else{
                    alert('出错信息:{0}'.format(data));
                }
            }
        });
    }
    function del_click(){
        var $this = $(this);
        if(confirm("确定从“我感兴趣的论坛”中删除?")){
            var $fid = $this.prev('a').attr('href').match(/fid=(\d+)$/)[1];
            $.ajax({
                url: '/frame/left_frame_content.php',
                cache: false,
                type: 'POST',
                data: {action: 'del', 'fid': $fid},
                success: function(data){
                    if(data=='ok'){
                        $this.parent().next('div[class!="depth1"]').remove();
                        $this.parent('div').hide(200, function(){
                            $this.parent().remove();
                            var $lastimg = $('.favoriteTree').children('.depth1').last().find('img[src*="middle.gif"]');
                            if($lastimg.length > 0){
                                $lastimg.attr('src', $lastimg.attr('src').replace('middle', 'bottom'));
                                $lastimg.parent().next().find('img[src*="tree_line.gif"]').each(function(){
                                    if(!$(this).next('img').attr('src').has('tree_line.gif')){
                                        $(this).attr('src', $(this).attr('src').replace('tree_line.gif', 'tree_empty.gif'));
                                    }
                                })
                            }
                            $('div.Tree a[href$="fid={0}"]'.format($fid)).after('<a class="add" href="###" title="添加到“我感兴趣的论坛”中">添加</a>').next().click(add_click);
                        });
                    }else{
                        alert('出错信息:{0}'.format(data));
                    }
                }
            });
        }
    }

    /**
     * “寻找我感兴趣的论坛”几个字闪亮几下
     */
    var $i = 0;
    function interest_highlight(){
        if($i%2==0){
            $('.interest').css('color', '#ccc');
        }else{
            $('.interest').css('color', 'red');
        }
        $i++;
        if($i<7){
            setTimeout(interest_highlight, 500);
        }
    }
    interest_highlight();

    var $interest_focus = 0;
    var $old_keyword = ' ';
    $('.interest').focus(function(){
        $interest_focus = 1;
        $('.floatTree').show();
        var $this = $(this);
        if($this.val()=='寻找我感兴趣的论坛'){
            $this.val('');
        }
        setInterval(function(){
            if($this.val()!=$old_keyword){
                getTree($this.val());
                $old_keyword = $this.val();
            }
        }, 5);
    });

    $('.interest').blur(function(){
        $interest_focus = 0;
        if($floattree_hover==0){
            $('.floatTree').hide();
            var $this = $(this);
            if($this.val()==''){
                $this.val('寻找我感兴趣的论坛');
            }
        }
    });

    $('.interest').hover(function(){
        $interest_focus = 1;
    });

    var $floattree_hover = 0;
    $('.floatTree').hover(function(){
        $floattree_hover = 1;
    }, function(){
        $floattree_hover = 0;
        setTimeout(function(){
            if($interest_focus==0){
                $('.floatTree').hide();
            }
        }, 200);
    });


    var $tree_cache = {};
    function getTree(keyword){
        if(keyword==''){
            keyword = ' ';
        }
        if(keyword in $tree_cache){
            applytree($tree_cache[keyword]);
        }else{
            $('.floatTree .floatDiv1 #loading').attr('src','/images/default/loading.gif');
            $.ajax({
                url: '/frame/left_frame_content.php',
                type: 'GET',
                data: {action:'gettree', keyword:keyword},
                success: function(data){
                    applytree(data);
                }
            });
        }
        function applytree(data){
            $('.floatTree .floatDiv1 #loading').attr('src','/frame/skin/img/tree_minusmiddle.gif');
            $('.floatTree .floatDiv2').html(data);
            $('.floatTree .floatDiv2 img').filter(function(){
                return $(this).attr('src').has_any(['plus', 'minus']);
            }).css({cursor:'pointer'}).click(plus_or_minus_click);
            $('.floatTree .floatDiv2 a[href*="forumdisplay.php?fid="]').filter(function(){
                return $('div.Tree a[href="{0}"]'.format($(this).attr('href'))).next('a:contains("添加")').length > 0;
            }).after('<a class="add" href="###" title="添加到“我感兴趣的论坛”中">添加</a>');
            $('.floatTree .floatDiv2 a:contains("添加")').click(add_click);
            $tree_cache[keyword] = data;
        }
    }
});

搜索更多相关主题的帖子: cursor folder filter pointer 
2012-09-23 22:38
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3451
专家分:19340
注 册:2012-3-31
收藏
得分:20 
大哥加油!!!

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-09-24 14:05
a462410594
Rank: 2
等 级:论坛游民
帖 子:75
专家分:64
注 册:2011-11-17
收藏
得分:0 
大哥,读网页设计(大专的)要考哪些证呀?
2012-09-26 20:10
a462410594
Rank: 2
等 级:论坛游民
帖 子:75
专家分:64
注 册:2011-11-17
收藏
得分:0 
看不懂啊
2012-09-27 23:14
lseyesl
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-15
收藏
得分:0 
多写些注释
2012-10-15 11:05
快速回复:jquery写的时候挺舒服,过几个月再读的时候就头大了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016033 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved