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; } } });