常用 ajax js 表单

$(function () {

    /**
     * 图片点击放大处理
     */
    $(‘.mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.img_info img‘).click(function () {
        $(this).parents(‘.img_tool‘).hide().prev().show();
    });
    $(‘.packup‘).click(function () {
        $(this).parent().parent().parent().hide().prev().show();
    });
    $(‘.turn_mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.turn_img_info img‘).click(function () {
        $(this).parents(‘.turn_img_tool‘).hide().prev().show();
    });

    /**
     * 转发框处理
     */
     $(‘.turn‘).click(function () {
         //获取原微内容并添加到转发框
         var orgObj = $(this).parents(‘.wb_tool‘).prev();
         var author = $.trim(orgObj.find(‘.author‘).html());
         var content = orgObj.find(‘.content p‘).html();
         var tid = $(this).attr(‘tid‘) ? $(this).attr(‘tid‘) : 0;
         var cons = ‘‘;

         //多重转发时,转发框内容处理
         if (tid) {
             author = orgObj.find(‘.author a‘).html();
             cons = replace_weibo(‘ // @‘ + author + ‘ : ‘ + content);
             author = $.trim(orgObj.find(‘.turn_name‘).html());
             content = orgObj.find(‘.turn_cons p‘).html();
         }

         $(‘.turn_main p‘).html(author + ‘ : ‘ + content);
         $(‘.turn-cname‘).html(author);
         $(‘form[name=turn] textarea‘).val(cons);

         //提取原微博ID
         $(‘form[name=turn] input[name=id]‘).val($(this).attr(‘id‘));
         $(‘form[name=turn] input[name=tid]‘).val(tid);

         //隐藏表情框
         $(‘#phiz‘).hide();
         //点击转发创建透明背景层
         createBg(‘opacity_bg‘);
         //定位转发框居中
         var turnLeft = ($(window).width() - $(‘#turn‘).width()) / 2;
         var turnTop = $(document).scrollTop() + ($(window).height() - $(‘#turn‘).height()) / 2;
         $(‘#turn‘).css({
             ‘left‘ : turnLeft,
             ‘top‘ : turnTop
         }).fadeIn().find(‘textarea‘).focus(function () {
             $(this).css(‘borderColor‘, ‘#FF9B00‘).keyup(function () {
                var content = $(this).val();
                var lengths = check(content);  //调用check函数取得当前字数
                //最大允许输入140个字
                if (lengths[0] >= 140) {
                    $(this).val(content.substring(0, Math.ceil(lengths[1])));
                }
                var num = 140 - Math.ceil(lengths[0]);
                var msg = num < 0 ? 0 : num;
                //当前字数同步到显示提示
                $(‘#turn_num‘).html(msg);
            });
         }).focus().blur(function () {
             $(this).css(‘borderColor‘, ‘#CCCCCC‘);    //失去焦点时还原边框颜色
         });
     });
    drag($(‘#turn‘), $(‘.turn_text‘));  //拖拽转发框

    /**
     * 收藏微博
     */
    $(‘.keep‘).click(function () {
        var wid = $(this).attr(‘wid‘);
        var keepUp = $(this).next();
        var msg = ‘‘;

        $.post(keepUrl, {wid : wid}, function (data) {
            if (data == 1) {
                msg = ‘收藏成功‘;
            }

            if (data == -1) {
                msg = ‘已收藏‘;
            }

            if (data == 0) {
                msg = ‘收藏失败‘;
            }

            keepUp.html(msg).fadeIn();
            setTimeout(function () {
                keepUp.fadeOut();
            }, 3000);

        }, ‘json‘);

    });

    /**
     * 评论框处理
     */
    //点击评论时异步提取数据
    $(‘.comment‘).toggle(function () {
        //异步加载状态DIV
        var commentLoad = $(this).parents(‘.wb_tool‘).next();
        var commentList = commentLoad.next();

        //提取当前评论按钮对应微博的ID号
        var wid = $(this).attr(‘wid‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    }, function () {
        $(this).parents(‘.wb_tool‘).next().next().hide().find(‘dl‘).remove();
        $(‘#phiz‘).hide();
    });
    //评论输入框获取焦点时改变边框颜色
    $(‘.comment_list textarea‘).focus(function () {
        $(this).css(‘borderColor‘, ‘#FF9B00‘);
    }).blur(function () {
        $(this).css(‘borderColor‘, ‘#CCCCCC‘);
    }).keyup(function () {
        var content = $(this).val();
        var lengths = check(content);  //调用check函数取得当前字数
        //最大允许输入140个字
        if (lengths[0] >= 140) {
            $(this).val(content.substring(0, Math.ceil(lengths[1])));
        }
    });
    //回复
    $(‘.reply a‘).live(‘click‘, function () {
        var reply = $(this).parent().siblings(‘a‘).html();
        $(this).parents(‘.comment_list‘).find(‘textarea‘).val(‘回复@‘ + reply + ‘ :‘);
        return false;
    });
    //提交评论
    $(‘.comment_btn‘).click(function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var _textarea = commentList.find(‘textarea‘);
        var content = _textarea.val();

        //评论内容为空时不作处理
        if (content == ‘‘) {
            _textarea.focus();
            return false;
        }

        //提取评论数据
        var cons = {
            content : content,
            wid : $(this).attr(‘wid‘),
            uid : $(this).attr(‘uid‘),
            isturn : $(this).prev().find(‘input:checked‘).val() ? 1 : 0
        };

        $.post(commentUrl, cons, function (data) {
            if (data != ‘false‘) {
                if (cons.isturn) {
                    window.location.reload();
                } else {
                    _textarea.val(‘‘);
                    commentList.find(‘ul‘).after(data);
                }
            } else {
                alert(‘评论失败,请重试...‘);
            }
        }, ‘html‘);
    });

    /**
     * 评论异步分类处理
     */
    $(‘.comment-page dd‘).live(‘click‘, function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var commentLoad = commentList.prev();
        var wid = $(this).attr(‘wid‘);
        var page = $(this).attr(‘page‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid, page : page},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentList.hide().find(‘dl‘).remove();
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    });

    /**
     * 取消收藏
     */
    $(‘.cancel-keep‘).click(function () {
        var isCancel = confirm(‘确认取消该微博的收藏?‘);
        var data = {
            kid : $(this).attr(‘kid‘),
            wid : $(this).attr(‘wid‘)
        };
        var obj = $(this).parents(‘.weibo‘);

        if (isCancel) {
            $.post(cancelKeep, data, function (data) {
                if (data) {
                    obj.slideUp(‘slow‘, function () {
                        obj.remove();
                    });
                } else {
                    alert(‘取消失败,请重试...‘);
                }
            }, ‘json‘);
        }
    });

    /**
     * 表情处理
     * 以原生JS添加点击事件,不走jQuery队列事件机制
     */
      var phiz = $(‘.phiz‘);
      for (var i = 0; i < phiz.length; i++) {
          phiz[i].onclick = function () {
              //定位表情框到对应位置
            $(‘#phiz‘).show().css({
                ‘left‘ : $(this).offset().left,
                ‘top‘ : $(this).offset().top + $(this).height() + 5
            });
            //为每个表情图片添加事件
            var phizImg = $("#phiz img");
            var sign = this.getAttribute(‘sign‘);
            for (var i = 0; i < phizImg.length; i++){
                phizImg[i].onclick = function () {
                var content = $(‘textarea[sign = ‘+sign+‘]‘);
                content.val(content.val() + ‘[‘ + $(this).attr(‘title‘) + ‘]‘);
                $(‘#phiz‘).hide();
                }
            }
          }
      }
      //关闭表情框
    $(‘.close‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-100px -200px‘);
    }, function () {
        $(this).css(‘backgroundPosition‘, ‘-75px -200px‘);
    }).click(function () {
        $(this).parent().parent().hide();
        $(‘#phiz‘).hide();
        if ($(‘#turn‘).css(‘display‘) == ‘none‘) {
            $(‘#opacity_bg‘).remove();
        };
    });

});

/**
 * 统计字数
 * @param  字符串
 * @return 数组[当前字数, 最大字数]
 */
function check (str) {
    var num = [0, 140];
    for (var i=0; i<str.length; i++) {
        //字符串不是中文时
        if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 255){
            num[0] = num[0] + 0.5;//当前字数增加0.5个
            num[1] = num[1] + 0.5;//最大输入字数增加0.5个
        } else {//字符串是中文时
            num[0]++;//当前字数增加1个
        }
    }
    return num;
}

/**
 * 替换微博内容,去除 <a> 链接与表情图片
 */
function replace_weibo (content) {
    content = content.replace(/<img.*?title=[‘"](.*?)[‘"].*?\/?>/ig, ‘[$1]‘);
    content = content.replace(/<a.*?>(.*?)<\/a>/ig, ‘$1‘);
    return content.replace(/<span.*?>\&nbsp;(\/\/)\&nbsp;<\/span>/ig, ‘$1‘);
}
$(function () {

    /**
     * 图片点击放大处理
     */
    $(‘.mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.img_info img‘).click(function () {
        $(this).parents(‘.img_tool‘).hide().prev().show();
    });
    $(‘.packup‘).click(function () {
        $(this).parent().parent().parent().hide().prev().show();
    });
    $(‘.turn_mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.turn_img_info img‘).click(function () {
        $(this).parents(‘.turn_img_tool‘).hide().prev().show();
    });

    /**
     * 转发框处理
     */
     $(‘.turn‘).click(function () {
         //获取原微内容并添加到转发框
         var orgObj = $(this).parents(‘.wb_tool‘).prev();
         var author = $.trim(orgObj.find(‘.author‘).html());
         var content = orgObj.find(‘.content p‘).html();
         var tid = $(this).attr(‘tid‘) ? $(this).attr(‘tid‘) : 0;
         var cons = ‘‘;

         //多重转发时,转发框内容处理
         if (tid) {
             author = orgObj.find(‘.author a‘).html();
             cons = replace_weibo(‘ // @‘ + author + ‘ : ‘ + content);
             author = $.trim(orgObj.find(‘.turn_name‘).html());
             content = orgObj.find(‘.turn_cons p‘).html();
         }

         $(‘.turn_main p‘).html(author + ‘ : ‘ + content);
         $(‘.turn-cname‘).html(author);
         $(‘form[name=turn] textarea‘).val(cons);

         //提取原微博ID
         $(‘form[name=turn] input[name=id]‘).val($(this).attr(‘id‘));
         $(‘form[name=turn] input[name=tid]‘).val(tid);

         //隐藏表情框
         $(‘#phiz‘).hide();
         //点击转发创建透明背景层
         createBg(‘opacity_bg‘);
         //定位转发框居中
         var turnLeft = ($(window).width() - $(‘#turn‘).width()) / 2;
         var turnTop = $(document).scrollTop() + ($(window).height() - $(‘#turn‘).height()) / 2;
         $(‘#turn‘).css({
             ‘left‘ : turnLeft,
             ‘top‘ : turnTop
         }).fadeIn().find(‘textarea‘).focus(function () {
             $(this).css(‘borderColor‘, ‘#FF9B00‘).keyup(function () {
                var content = $(this).val();
                var lengths = check(content);  //调用check函数取得当前字数
                //最大允许输入140个字
                if (lengths[0] >= 140) {
                    $(this).val(content.substring(0, Math.ceil(lengths[1])));
                }
                var num = 140 - Math.ceil(lengths[0]);
                var msg = num < 0 ? 0 : num;
                //当前字数同步到显示提示
                $(‘#turn_num‘).html(msg);
            });
         }).focus().blur(function () {
             $(this).css(‘borderColor‘, ‘#CCCCCC‘);    //失去焦点时还原边框颜色
         });
     });
    drag($(‘#turn‘), $(‘.turn_text‘));  //拖拽转发框

    /**
     * 收藏微博
     */
    $(‘.keep‘).click(function () {
        var wid = $(this).attr(‘wid‘);
        var keepUp = $(this).next();
        var msg = ‘‘;

        $.post(keepUrl, {wid : wid}, function (data) {
            if (data == 1) {
                msg = ‘收藏成功‘;
            }

            if (data == -1) {
                msg = ‘已收藏‘;
            }

            if (data == 0) {
                msg = ‘收藏失败‘;
            }

            keepUp.html(msg).fadeIn();
            setTimeout(function () {
                keepUp.fadeOut();
            }, 3000);

        }, ‘json‘);

    });

    /**
     * 评论框处理
     */
    //点击评论时异步提取数据
    $(‘.comment‘).toggle(function () {
        //异步加载状态DIV
        var commentLoad = $(this).parents(‘.wb_tool‘).next();
        var commentList = commentLoad.next();

        //提取当前评论按钮对应微博的ID号
        var wid = $(this).attr(‘wid‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    }, function () {
        $(this).parents(‘.wb_tool‘).next().next().hide().find(‘dl‘).remove();
        $(‘#phiz‘).hide();
    });
    //评论输入框获取焦点时改变边框颜色
    $(‘.comment_list textarea‘).focus(function () {
        $(this).css(‘borderColor‘, ‘#FF9B00‘);
    }).blur(function () {
        $(this).css(‘borderColor‘, ‘#CCCCCC‘);
    }).keyup(function () {
        var content = $(this).val();
        var lengths = check(content);  //调用check函数取得当前字数
        //最大允许输入140个字
        if (lengths[0] >= 140) {
            $(this).val(content.substring(0, Math.ceil(lengths[1])));
        }
    });
    //回复
    $(‘.reply a‘).live(‘click‘, function () {
        var reply = $(this).parent().siblings(‘a‘).html();
        $(this).parents(‘.comment_list‘).find(‘textarea‘).val(‘回复@‘ + reply + ‘ :‘);
        return false;
    });
    //提交评论
    $(‘.comment_btn‘).click(function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var _textarea = commentList.find(‘textarea‘);
        var content = _textarea.val();

        //评论内容为空时不作处理
        if (content == ‘‘) {
            _textarea.focus();
            return false;
        }

        //提取评论数据
        var cons = {
            content : content,
            wid : $(this).attr(‘wid‘),
            uid : $(this).attr(‘uid‘),
            isturn : $(this).prev().find(‘input:checked‘).val() ? 1 : 0
        };

        $.post(commentUrl, cons, function (data) {
            if (data != ‘false‘) {
                if (cons.isturn) {
                    window.location.reload();
                } else {
                    _textarea.val(‘‘);
                    commentList.find(‘ul‘).after(data);
                }
            } else {
                alert(‘评论失败,请重试...‘);
            }
        }, ‘html‘);
    });

    /**
     * 评论异步分类处理
     */
    $(‘.comment-page dd‘).live(‘click‘, function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var commentLoad = commentList.prev();
        var wid = $(this).attr(‘wid‘);
        var page = $(this).attr(‘page‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid, page : page},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentList.hide().find(‘dl‘).remove();
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    });

    /**
     * 表情处理
     * 以原生JS添加点击事件,不走jQuery队列事件机制
     */
      var phiz = $(‘.phiz‘);
      for (var i = 0; i < phiz.length; i++) {
          phiz[i].onclick = function () {
              //定位表情框到对应位置
            $(‘#phiz‘).show().css({
                ‘left‘ : $(this).offset().left,
                ‘top‘ : $(this).offset().top + $(this).height() + 5
            });
            //为每个表情图片添加事件
            var phizImg = $("#phiz img");
            var sign = this.getAttribute(‘sign‘);
            for (var i = 0; i < phizImg.length; i++){
                phizImg[i].onclick = function () {
                var content = $(‘textarea[sign = ‘+sign+‘]‘);
                content.val(content.val() + ‘[‘ + $(this).attr(‘title‘) + ‘]‘);
                $(‘#phiz‘).hide();
                }
            }
          }
      }
      //关闭表情框
    $(‘.close‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-100px -200px‘);
    }, function () {
        $(this).css(‘backgroundPosition‘, ‘-75px -200px‘);
    }).click(function () {
        $(this).parent().parent().hide();
        $(‘#phiz‘).hide();
        if ($(‘#turn‘).css(‘display‘) == ‘none‘) {
            $(‘#opacity_bg‘).remove();
        };
    });

});

/**
 * 统计字数
 * @param  字符串
 * @return 数组[当前字数, 最大字数]
 */
function check (str) {
    var num = [0, 140];
    for (var i=0; i<str.length; i++) {
        //字符串不是中文时
        if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 255){
            num[0] = num[0] + 0.5;//当前字数增加0.5个
            num[1] = num[1] + 0.5;//最大输入字数增加0.5个
        } else {//字符串是中文时
            num[0]++;//当前字数增加1个
        }
    }
    return num;
}

/**
 * 替换微博内容,去除 <a> 链接与表情图片
 */
function replace_weibo (content) {
    content = content.replace(/<img.*?title=[‘"](.*?)[‘"].*?\/?>/ig, ‘[$1]‘);
    content = content.replace(/<a.*?>(.*?)<\/a>/ig, ‘$1‘);
    return content.replace(/<span.*?>\&nbsp;(\/\/)\&nbsp;<\/span>/ig, ‘$1‘);
}
$(function () {

    //点击刷新验证码
    var verifyUrl = $(‘#verify-img‘).attr(‘src‘);
    $(‘#verify-img‘).click(function () {
        $(this).attr(‘src‘, verifyUrl + ‘/‘ + Math.random());
    });

    //jQuery Validate 表单验证

    /**
     * 添加验证方法
     * 以字母开头,5-17 字母、数字、下划线"_"
     */
    jQuery.validator.addMethod("user", function(value, element) {
        var tel = /^[a-zA-Z][\w]{4,16}$/;
        return this.optional(element) || (tel.test(value));
    }, "以字母开头,5-17 字母、数字、下划线‘_‘");

    $(‘form[name=register]‘).validate({
        errorElement : ‘span‘,
        success : function (label) {
            label.addClass(‘success‘);
        },
        rules : {
            account : {
                required : true,
                user : true,
                remote : {
                    url : checkAccount,
                    type : ‘post‘,
                    dataType : ‘json‘,
                    data : {
                        account : function () {
                            return $(‘#account‘).val();
                        }
                    }
                }
            },
            pwd : {
                required : true,
                user : true
            },
            pwded : {
                required : true,
                equalTo : "#pwd"
            },
            uname : {
                required : true,
                rangelength : [2,10],
                remote : {
                    url : checkUname,
                    type : ‘post‘,
                    dataType : ‘json‘,
                    data : {
                        uname : function () {
                            return $(‘#uname‘).val();
                        }
                    }
                }
            },
            verify : {
                required : true,
                remote : {
                    url : checkVerify,
                    type : ‘post‘,
                    dataType : ‘json‘,
                    data : {
                        verify : function () {
                            return $(‘#verify‘).val();
                        }
                    }
                }
            }
        },
        messages : {
            account : {
                required : ‘账号不能为空‘,
                remote : ‘账号已存在‘
            },
            pwd : {
                required : ‘密码不能为空‘
            },
            pwded : {
                required : ‘请确认密码‘,
                equalTo : ‘两次密码不一致‘
            },
            uname : {
                required : ‘请填写您的昵称‘,
                rangelength : ‘昵称在2-10个字之间‘,
                remote : ‘昵称已存在‘
            },
            verify : {
                required : ‘ ‘,
                remote : ‘ ‘
            }
        }
    });

});
/**
 * 头部导航
 */
$(function () {
    /**
     * 头部选项移入效果
     */
    //左侧选项
    $(‘.top_left li‘).hover(function () {
        $(this).addClass(‘cur_bg‘);
    }, function () {
        $(this).removeClass(‘cur_bg‘);
    });
    //用户名
    $(‘.user‘).hover(function () {
        $(this).addClass(‘cur_bg‘);
    }, function () {
        $(this).removeClass(‘cur_bg‘);
    });
    //快速发微博按钮
    $(‘.top_right li:eq(0)‘).hover(function () {
        $(this).addClass(‘cur_bg‘);
    }, function () {
        $(this).removeClass(‘cur_bg‘);
    });
    $(‘.fast_send‘).click(function () {
        $(‘.send_write textarea‘).focus();
        $(‘.backToTop‘).click();
    });

    /**
     * 头部右侧下拉选项
     */
    $(‘.selector‘).hover(function () {
        var objClass = $(‘i‘, this).attr(‘class‘);
        $(‘i‘, this).removeClass(objClass).addClass(objClass + ‘-cur‘);
        $(this).css({  //改变背景色
            ‘width‘ : ‘36px‘,
            ‘backgroundColor‘ : ‘#FFFFFF‘,
            ‘borderLeft‘ : ‘1px solid #CCCCCC‘,
            ‘borderRight‘ : ‘1px solid #CCCCCC‘
        }).find(‘ul‘).show();
    }, function () {
        var objClass = $(‘i‘, this).attr(‘class‘);
        $(‘i‘, this).removeClass(objClass).addClass(objClass.replace(‘-cur‘, ‘‘));
        $(this).css({  //还原背景
            ‘width‘ : ‘38px‘,
            ‘background‘ : ‘none‘,
            ‘border‘ : ‘none‘
        }).find(‘ul‘).hide();
    });
    $(‘.selector li‘).hover(function () {  //下拉项添加效果
        $(this).css(‘background‘, ‘#DCDCDC‘);
    }, function () {
        $(this).css(‘background‘, ‘none‘);
    });

    /**
     * 头部搜索框
     */
    //移入时改变背景
    $(‘#sech_text‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-237px -5px‘);
        $(‘#sech_sub‘).css(‘backgroundPosition‘, ‘-443px -5px‘);
    }, function () {
        if ($(this).val() == ‘搜索微博、找人‘) {
            $(this).css(‘backgroundPosition‘, ‘0 -5px‘);
            $(‘#sech_sub‘).css(‘backgroundPosition‘, ‘-206px -5px‘);
        };
    //获得焦点时清空默认文字
    }).focus(function () {
        if ($(this).val() == ‘搜索微博、找人‘) {
            $(this).val(‘‘);
        };
    //失去焦点时
    }).blur(function () {
        //添加默认文字
        if ($(this).val() == ‘‘) {
            $(this).val(‘搜索微博、找人‘)
        };
        //恢复原背景
        $(this).css(‘backgroundPosition‘, ‘0 -5px‘);
        $(‘#sech_sub‘).css(‘backgroundPosition‘, ‘-206px -5px‘);
    });
    $(‘#sech_sub‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-443px -5px‘);
        $(‘#sech_text‘).css(‘backgroundPosition‘, ‘-237px -5px‘);
    }, function () {
        $(this).css(‘backgroundPosition‘, ‘-206px -5px‘);
        $(‘#sech_text‘).css(‘backgroundPosition‘, ‘0 -5px‘);
    });

    /**
     * 中部左侧导行选项移入效果
     */
    $(‘.left_nav li‘).hover(function () {
        $(this).css(‘background‘, ‘#D7ECF4‘);
    }, function () {
        $(this).css(‘background‘, ‘#EFF8FC‘);
    });
    $(‘.group ul li‘).hover(function () {
        $(this).css(‘background‘, ‘#D7ECF4‘);
    }, function () {
        $(this).css(‘background‘, ‘#EFF8FC‘);
    });

    /**
     * 返回顶部
     */
    var toTopElement = ‘<div class="backToTop" title="返回顶部"><i class="icon icon-totop"></i>返回顶部</div>‘;
    //创建DIV按钮并定位
    var toTop = $(toTopElement).appendTo($("body")).css({
        ‘left‘ : ($(‘body‘).width() - ($(‘body‘).width() - $(‘.main‘).width()) / 2) + ‘px‘,
        ‘top‘ : ($(window).height() - ($(window).height() / 3)) + 80 + ‘px‘
    //添加点击事件
    }).click(function() {
        $("html, body").animate({scrollTop: 0}, 200);
    });
    //添加窗口滚动事件
    $(window).scroll(function () {
        var st = $(document).scrollTop();
        //IE6定位
        if (window.ActiveXObject&&!window.XMLHttpRequest) {
            var ieTop = st + ($(window).height() / 2 + 80);
            $(‘.backToTop‘).css(‘top‘, ieTop + ‘px‘);
        }
        //滚动条高度大于100时显示 返回顶部按钮
        (st > 100) ? $(‘.backToTop‘).show() : $(‘.backToTop‘).hide();
    });

    //创建好友分组
   $(‘#create_group‘).click(function () {
           var groupLeft = ($(window).width() - $(‘#add-group‘).width()) / 2;
         var groupTop = $(document).scrollTop() + ($(window).height() - $(‘#add-group‘).height()) / 2;
           var gpObj = $(‘#add-group‘).show().css({
             ‘left‘ : groupLeft,
             ‘top‘ : groupTop
         });
           createBg(‘group-bg‘);
           drag(gpObj, gpObj.find(‘.group_head‘));
   });
   //异步创建分组
   $(‘.add-group-sub‘).click(function () {
           var groupName = $(‘#gp-name‘).val();
           if (groupName != ‘‘) {
               $.post(addGroup, {name : groupName}, function (data) {
                   if (data.status) {
                       showTips(data.msg);
                       $(‘#add-group‘).hide();
                       $(‘#group-bg‘).remove();
                   } else {
                       alert(data.msg);
                   }
               }, ‘json‘);
           }
   });
   //关闭
   $(‘.group-cencle‘).click(function () {
           $(‘#add-group‘).hide();
           $(‘#group-bg‘).remove();
   });

    //好友关注
   $(‘.add-fl‘).click(function () {
           var followLeft = ($(window).width() - $(‘#follow‘).width()) / 2;
         var followTop = $(document).scrollTop() + ($(window).height() - $(‘#follow‘).height()) / 2;
           var flObj = $(‘#follow‘).show().css({
             ‘left‘ : followLeft,
             ‘top‘ : followTop
         });
           createBg(‘follow-bg‘);
           drag(flObj, flObj.find(‘.follow_head‘));
           $(‘input[name=follow]‘).val($(this).attr(‘uid‘));
   });
   //添加关注
   $(‘.add-follow-sub‘).click(function () {
           var follow = $(‘input[name=follow]‘).val();
           var group = $(‘select[name=gid]‘).val();
           $.post(addFollow, {
               ‘follow‘ : follow,
               ‘gid‘ : group
           }, function (data) {
               if (data.status) {
                   $(‘.add-fl[uid=‘ + follow + ‘]‘).removeClass(‘add-fl‘).html(‘√&nbsp;已关注‘);
                   $(‘#follow‘).hide();
                   $(‘#follow-bg‘).remove();
               } else {
                   alert(data.msg);
               }
           }, ‘json‘);
   });
   //关闭关注框
   $(‘.follow-cencle‘).click(function () {
           $(‘#follow‘).hide();
           $(‘#follow-bg‘).remove();
   });

   //移除关注与粉丝
   $(‘.del-follow‘).click(function () {
           var data = {
               uid : $(this).attr(‘uid‘),
               type : $(this).attr(‘type‘)
           };
           var isDel = confirm(‘确认移除?‘);
           var obj = $(this).parents(‘li‘);

           if (isDel) {
               $.post(delFollow, data, function (data) {
                   if (data) {
                       obj.slideUp(‘slow‘, function () {
                           obj.remove();
                       })
                   } else {
                       alert(‘移除失败请重试...‘);
                   }
               }, ‘json‘);
           }
   });

   //搜索切换
   $(‘.sech-type‘).click(function () {
           $(‘.cur‘).removeClass(‘cur‘);
           $(this).addClass(‘cur‘);
           $(‘form[name=search]‘).attr(‘action‘, $(this).attr(‘url‘));
   });

       /**
     * 自定义模版框
     */
     $(‘.set_model‘).click(function () {
         //点击转发创建透明背景层
         createBg(‘opacity_bg‘);
         //定位模版选择框居中
         var modelLeft = ($(window).width() - $(‘#model‘).width()) / 2;
         var modelTop = $(document).scrollTop() + ($(window).height() - $(‘#model‘).height()) / 2;
         $(‘#model‘).css({
             ‘left‘ : modelLeft,
             ‘top‘ : modelTop
         }).fadeIn();
         return false;
     });
     //点击消取时
     $(‘.model_cancel‘).click(function () {
        $(‘#model‘).hide();
        $(‘#opacity_bg‘).remove();
     });
     drag($(‘#model‘), $(‘.model_text‘));  //拖拽模版框

     //选中模版风格
     $(‘#model ul li‘).click(function () {
         $(this).addClass(‘theme-cur‘).siblings().removeClass(‘theme-cur‘);
     });

     //保存模版风格
     $(‘#model .model_save‘).click(function () {
         var theme = $(‘.theme-cur‘).attr(‘theme‘);

         if (!theme) {
             alert(‘请选择一套模版风格‘);
         } else {
             $.post(editStyle, {style : theme}, function (data) {
                 if (data) {
                     window.location.reload();
                 } else {
                     alert(‘修改失败请重试...‘);
                 }
             }, ‘json‘);
         }
     })

    //消息推送回调函数
    get_msg(getMsgUrl);

});

/********************效果函数********************/

/**
 * 异步轮询函数
 */
function get_msg (url) {
    $.getJSON(url, function (data) {
        if (data.status) {
           news({
                "total" : data.total,
                "type" : data.type
            });
        }
        setTimeout(function () {
            get_msg(url);
        }, 5000);
    });
}

/**
 * 推送的新消息
 * @param  {[type]} json {total:新消息的条数,type:(1:评论,2:私信,3:@我)}
 * @return {[type]}      [description]
 */
var flags = true;
function news (json) {
    switch (json.type) {
        case 1:
            $(‘#news ul .news_comment‘).show().find(‘a‘).html(json.total + ‘条新评论‘);
            break;
        case 2:
            $(‘#news ul .news_letter‘).show().find(‘a‘).html(json.total + ‘条新私信‘);
            break;
        case 3:
            $(‘#news ul .news_atme‘).show().find(‘a‘).html(json.total + ‘条@提到我‘);
            break;
    }
    var obj = $(‘#news‘);
    var icon = obj.find(‘i‘);
    obj.show().find(‘li‘).hover(function () {  //下拉项添加效果
        $(this).css(‘background‘, ‘#DCDCDC‘);
    }, function () {
        $(this).css(‘background‘, ‘none‘);
    }).click(function () {
        clearInterval(newsGlint);
    });
    if (flags) {
        flags = false;
        var newsGlint= setInterval(function () {
            icon.toggleClass("icon-news");
        }, 500);
    }
}

/**
 * 创建全屏透明背景层
 * @param   id
 */
function createBg (id) {
    $(‘<div id = "‘ + id + ‘"></div>‘).appendTo(‘body‘).css({
         ‘width‘ : $(document).width(),
         ‘height‘ : $(document).height(),
         ‘position‘ : ‘absolute‘,
         ‘top‘ : 0,
         ‘left‘ : 0,
         ‘z-index‘ : 2,
         ‘opacity‘ : 0.3,
         ‘filter‘ : ‘Alpha(Opacity = 30)‘,
         ‘backgroundColor‘ : ‘#000‘
     });
}

/**
* 元素拖拽
* @param  obj        拖拽的对象
* @param  element     触发拖拽的对象
*/
function drag (obj, element) {
    var DX, DY, moving;
    element.mousedown(function (event) {
        DX = event.pageX - parseInt(obj.css(‘left‘));    //鼠标距离事件源宽度
        DY = event.pageY - parseInt(obj.css(‘top‘));    //鼠标距离事件源高度
        moving = true;    //记录拖拽状态
    });
    $(document).mousemove(function (event) {
        if (!moving) return;
        var OX = event.pageX, OY = event.pageY;    //移动时鼠标当前 X、Y 位置
        var    OW = obj.outerWidth(), OH = obj.outerHeight();    //拖拽对象宽、高
        var DW = $(window).width(), DH = $(‘body‘).height();  //页面宽、高
        var left, top;    //计算定位宽、高
        left = OX - DX < 0 ? 0 : OX - DX > DW - OW ? DW - OW : OX - DX;
        top = OY - DY < 0 ? 0 : OY - DY > DH - OH ? DH - OH : OY - DY;
        obj.css({
            ‘left‘ : left + ‘px‘,
            ‘top‘ : top + ‘px‘
        });
    }).mouseup(function () {
        moving = false;    //鼠标抬起消取拖拽状态
    });
}

/**操作成功效果**/
function showTips(tips,time,height){
    var windowWidth = $(window).width();height=height?height:$(window).height();
    time = time ? time : 1;
    var tipsDiv = ‘<div class="tipsClass">‘ + tips + ‘</div>‘;
    $( ‘body‘ ).append( tipsDiv );
    $( ‘div.tipsClass‘ ).css({
        ‘top‘ : height/2 + ‘px‘,
        ‘left‘ : ( windowWidth / 2 ) - 100 + ‘px‘,
        ‘position‘ : ‘absolute‘,
        ‘padding‘ : ‘3px 5px‘,
        ‘background‘: ‘#670768‘,
        ‘font-size‘ : 14 + ‘px‘,
        ‘text-align‘: ‘center‘,
        ‘width‘ : ‘300px‘,
        ‘height‘ : ‘40px‘,
        ‘line-height‘ : ‘40px‘,
        ‘color‘ : ‘#fff‘,
        ‘font-weight‘ : ‘bold‘,
        ‘opacity‘ : ‘0.8‘
    }).show();
    setTimeout( function(){
        $( ‘div.tipsClass‘ ).animate({
            top: height/2-50+‘px‘
        }, "slow").fadeOut();
    }, time * 1000);
}
$(function () {
    //jQuery Validate 表单验证

    /**
     * 添加验证方法
     * 以字母开头,5-17 字母、数字、下划线"_"
     */
    jQuery.validator.addMethod("user", function(value, element) {
        var tel = /^[a-zA-Z][\w]{4,16}$/;
        return this.optional(element) || (tel.test(value));
    }, " ");

    $(‘form[name=login]‘).validate({
        errorElement : ‘span‘,
        success : function (label) {
            label.addClass(‘success‘);
        },
        rules : {
            account : {
                required : true,
                user : true
            },
            pwd : {
                required : true,
                user : true
            }
        },
        messages : {
            account : {
                required : ‘ ‘,
            },
            pwd : {
                required : ‘ ‘
            }
        }
    });
});
$(function () {
    //发送私信框
   $(‘.l-reply,.send‘).click(function () {
      var username = ‘‘;

      if ($(this).attr(‘class‘) == ‘l-reply‘) {
         username = $(this).parents(‘dd‘).prev().find(‘a‘).html();
      }

       var letterLeft = ($(window).width() - $(‘#letter‘).width()) / 2;
         var letterTop = $(document).scrollTop() + ($(window).height() - $(‘#letter‘).height()) / 2;
        var obj = $(‘#letter‘).show().css({
             ‘left‘ : letterLeft,
             ‘top‘ : letterTop
         });

      obj.find(‘input[name=name]‘).val(username);
      obj.find(‘textarea‘).focus();
        createBg(‘letter-bg‘);
        drag(obj, obj.find(‘.letter_head‘));
   });
   //关闭
   $(‘.letter-cencle‘).click(function () {
           $(‘#letter‘).hide();
           $(‘#letter-bg‘).remove();
   });

   /**
    * 删除私信
    */
   $(‘.del-letter‘).click(function () {
      var isDel = confirm(‘确定删除该私信?‘);
      var lid = $(this).attr(‘lid‘);   //添加属性
      var obj = $(this).parents(‘dl‘);  //parents() 获得当前匹配元素集合中每个元素的祖先元素,使用选择器进行筛选是可选的。

      if (isDel) {
         $.post(delLetter, {lid : lid}, function (data) {
            if (data) {
               obj.slideUp(‘slow‘, function () {
                  obj.remove();
               });
            } else {
               alert(‘删除失败重请试...‘);
            }
         }, ‘json‘);
      }
   })
})
/**
 * 首页
 * @author Carmen
 */
$(function () {

    /**
     * 上传微博图片
     */
    $(‘#picture‘).uploadify({
        swf : PUBLIC + ‘/Uploadify/uploadify.swf‘,    //引入Uploadify核心Flash文件
        uploader : uploadUrl,    //PHP处理脚本地址
        width : 120,    //上传按钮宽度
        height : 30,    //上传按钮高度
        buttonImage : PUBLIC + ‘/Uploadify/browse-btn.png‘,    //上传按钮背景图地址
        fileTypeDesc : ‘Image File‘,    //选择文件提示文字
        fileTypeExts : ‘*.jpeg; *.jpg; *.png; *.gif‘,    //允许选择的文件类型
        formData : {‘session_id‘ : sid},
        //上传成功后的回调函数
        onUploadSuccess : function (file, data, response) {
            eval(‘var data = ‘ + data);
            if (data.status) {
                $(‘input[name=max]‘).val(data.path.max);
                $(‘input[name=medium]‘).val(data.path.medium);
                $(‘input[name=mini]‘).val(data.path.mini);

                $(‘#upload_img‘).fadeOut().next().fadeIn().find(‘img‘).attr(‘src‘, ROOT + ‘/Uploads/Pic/‘ + data.path.medium);
            } else {
                alert(data.msg);
            }
        }
    });

    /**
     * 发布转入框效果
     */
    $(‘.send_write textarea‘).focus(function () {
        //获取焦点时改变边框背景
        $(‘.ta_right‘).css(‘backgroundPosition‘, ‘0 -50px‘);
        //转入文字时
        $(this).css(‘borderColor‘, ‘#FFB941‘).keyup(function () {
            var content = $(this).val();
            //调用check函数取得当前字数
            var lengths = check(content);
            if (lengths[0] > 0) {//当前有输入内容时改变发布按钮背景
                $(‘.send_btn‘).css(‘backgroundPosition‘, ‘-133px -50px‘);
            } else {//内容为空时发布按钮背景归位
                $(‘.send_btn‘).css(‘backgroundPosition‘, ‘-50px -50px‘);
            };
            //最大允许输入140字个
            if (lengths[0] >= 140) {
                $(this).val(content.substring(0, Math.ceil(lengths[1])));
            }
            var num = 140 - Math.ceil(lengths[0]);
            var msg = num < 0 ? 0 : num;
            //当前字数同步到显示提示
            $(‘#send_num‘).html(msg);
        });
    //失去焦点时边框背景归位
    }).blur(function () {
        $(this).css(‘borderColor‘, ‘#CCCCCC‘);
        $(‘.ta_right‘).css(‘backgroundPosition‘, ‘0 -69px‘);
    });
    //内容提交时处理
    $(‘form[name=weibo]‘).submit(function () {
        var cons = $(‘textarea‘, this);
        if (cons.val() == ‘‘) {//内容为空时闪烁输入框
            var timeOut = 0;
            var glint = setInterval(function () {
                if (timeOut % 2 == 0) {
                    cons.css(‘background‘,‘#FFA0C2‘);
                } else {
                    cons.css(‘background‘,‘#fff‘);
                }
                timeOut++;
                if (timeOut > 7) {
                    clearInterval(glint);
                    cons.focus();
                }
            }, 100);
            return false;
        }
    });
    //显示图片上传框
    $(‘.icon-picture‘).click(function () {
        $(‘#phiz‘).hide();
        $(‘#upload_img‘).show();
    });

    /**
     * 图片点击放大处理
     */
    $(‘.mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.img_info img‘).click(function () {
        $(this).parents(‘.img_tool‘).hide().prev().show();
    });
    $(‘.packup‘).click(function () {
        $(this).parent().parent().parent().hide().prev().show();
    });
    $(‘.turn_mini_img‘).click(function () {
        $(this).hide().next().show();
    });
    $(‘.turn_img_info img‘).click(function () {
        $(this).parents(‘.turn_img_tool‘).hide().prev().show();
    });

    /**
     * 转发框处理
     */
     $(‘.turn‘).click(function () {
         //获取原微内容并添加到转发框
         var orgObj = $(this).parents(‘.wb_tool‘).prev();
         var author = $.trim(orgObj.find(‘.author‘).html());
         var content = orgObj.find(‘.content p‘).html();
         var tid = $(this).attr(‘tid‘) ? $(this).attr(‘tid‘) : 0;
         var cons = ‘‘;

         //多重转发时,转发框内容处理
         if (tid) {
             author = orgObj.find(‘.author a‘).html();
             cons = replace_weibo(‘ // @‘ + author + ‘ : ‘ + content);
             author = $.trim(orgObj.find(‘.turn_name‘).html());
             content = orgObj.find(‘.turn_cons p‘).html();
         }

         $(‘.turn_main p‘).html(author + ‘ : ‘ + content);
         $(‘.turn-cname‘).html(author);
         $(‘form[name=turn] textarea‘).val(cons);

         //提取原微博ID
         $(‘form[name=turn] input[name=id]‘).val($(this).attr(‘id‘));
         $(‘form[name=turn] input[name=tid]‘).val(tid);

         //隐藏表情框
         $(‘#phiz‘).hide();
         //点击转发创建透明背景层
         createBg(‘opacity_bg‘);
         //定位转发框居中
         var turnLeft = ($(window).width() - $(‘#turn‘).width()) / 2;
         var turnTop = $(document).scrollTop() + ($(window).height() - $(‘#turn‘).height()) / 2;
         $(‘#turn‘).css({
             ‘left‘ : turnLeft,
             ‘top‘ : turnTop
         }).fadeIn().find(‘textarea‘).focus(function () {
             $(this).css(‘borderColor‘, ‘#FF9B00‘).keyup(function () {
                var content = $(this).val();
                var lengths = check(content);  //调用check函数取得当前字数
                //最大允许输入140个字
                if (lengths[0] >= 140) {
                    $(this).val(content.substring(0, Math.ceil(lengths[1])));
                }
                var num = 140 - Math.ceil(lengths[0]);
                var msg = num < 0 ? 0 : num;
                //当前字数同步到显示提示
                $(‘#turn_num‘).html(msg);
            });
         }).focus().blur(function () {
             $(this).css(‘borderColor‘, ‘#CCCCCC‘);    //失去焦点时还原边框颜色
         });
     });
    drag($(‘#turn‘), $(‘.turn_text‘));  //拖拽转发框

    /**
     * 收藏微博
     */
    $(‘.keep‘).click(function () {
        var wid = $(this).attr(‘wid‘);
        var keepUp = $(this).next();
        var msg = ‘‘;

        $.post(keepUrl, {wid : wid}, function (data) {
            if (data == 1) {
                msg = ‘收藏成功‘;
            }

            if (data == -1) {
                msg = ‘已收藏‘;
            }

            if (data == 0) {
                msg = ‘收藏失败‘;
            }

            keepUp.html(msg).fadeIn();
            setTimeout(function () {
                keepUp.fadeOut();
            }, 3000);

        }, ‘json‘);

    });

    /**
     * 评论框处理
     */
    //点击评论时异步提取数据
    $(‘.comment‘).toggle(function () {
        //异步加载状态DIV
        var commentLoad = $(this).parents(‘.wb_tool‘).next();
        var commentList = commentLoad.next();

        //提取当前评论按钮对应微博的ID号
        var wid = $(this).attr(‘wid‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    }, function () {
        $(this).parents(‘.wb_tool‘).next().next().hide().find(‘dl‘).remove();
        $(‘#phiz‘).hide();
    });
    //评论输入框获取焦点时改变边框颜色
    $(‘.comment_list textarea‘).focus(function () {
        $(this).css(‘borderColor‘, ‘#FF9B00‘);
    }).blur(function () {
        $(this).css(‘borderColor‘, ‘#CCCCCC‘);
    }).keyup(function () {
        var content = $(this).val();
        var lengths = check(content);  //调用check函数取得当前字数
        //最大允许输入140个字
        if (lengths[0] >= 140) {
            $(this).val(content.substring(0, Math.ceil(lengths[1])));
        }
    });
    //回复
    $(‘.reply a‘).live(‘click‘, function () {
        var reply = $(this).parent().siblings(‘a‘).html();
        $(this).parents(‘.comment_list‘).find(‘textarea‘).val(‘回复@‘ + reply + ‘ :‘);
        return false;
    });
    //提交评论
    $(‘.comment_btn‘).click(function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var _textarea = commentList.find(‘textarea‘);
        var content = _textarea.val();

        //评论内容为空时不作处理
        if (content == ‘‘) {
            _textarea.focus();
            return false;
        }

        //提取评论数据
        var cons = {
            content : content,
            wid : $(this).attr(‘wid‘),
            uid : $(this).attr(‘uid‘),
            isturn : $(this).prev().find(‘input:checked‘).val() ? 1 : 0
        };

        $.post(commentUrl, cons, function (data) {
            if (data != ‘false‘) {
                if (cons.isturn) {
                    window.location.reload();
                } else {
                    _textarea.val(‘‘);
                    commentList.find(‘ul‘).after(data);
                }
            } else {
                alert(‘评论失败,请重试...‘);
            }
        }, ‘html‘);
    });

    /**
     * 评论异步分类处理
     */
    $(‘.comment-page dd‘).live(‘click‘, function () {
        var commentList = $(this).parents(‘.comment_list‘);
        var commentLoad = commentList.prev();
        var wid = $(this).attr(‘wid‘);
        var page = $(this).attr(‘page‘);
        //异步提取评论内容
        $.ajax({
            url : getComment,
            data : {wid : wid, page : page},
            dataType : ‘html‘,
            type : ‘post‘,
            beforeSend : function () {
                commentList.hide().find(‘dl‘).remove();
                commentLoad.show();
            },
            success : function (data) {
                if (data != ‘false‘) {
                    commentList.append(data);
                }
            },
            complete : function () {
                commentLoad.hide();
                commentList.show().find(‘textarea‘).val(‘‘).focus();
            }
        });
    });

    /**
     * 删除微博
     */
    $(‘.weibo‘).hover(function () {
        $(this).find(‘.del-li‘).show();
    }, function () {
        $(this).find(‘.del-li‘).hide();
    });
    $(‘.del-weibo‘).click(function () {
        var wid = $(this).attr(‘wid‘);
        var isDel = confirm(‘确认要删除该微博?‘);
        var obj = $(this).parents(‘.weibo‘);

        if (isDel) {
            $.post(delWeibo, {wid : wid}, function (data) {
                if (data) {
                    obj.slideUp(‘slow‘, function () {
                        obj.remove();
                    });
                } else {
                    alert(‘删除失败请重试...‘);
                }
            }, ‘json‘);
        }
    });

    /**
     * 表情处理
     * 以原生JS添加点击事件,不走jQuery队列事件机制
     */
      var phiz = $(‘.phiz‘);
      for (var i = 0; i < phiz.length; i++) {
          phiz[i].onclick = function () {
              //定位表情框到对应位置
            $(‘#phiz‘).show().css({
                ‘left‘ : $(this).offset().left,
                ‘top‘ : $(this).offset().top + $(this).height() + 5
            });
            //为每个表情图片添加事件
            var phizImg = $("#phiz img");
            var sign = this.getAttribute(‘sign‘);
            for (var i = 0; i < phizImg.length; i++){
                phizImg[i].onclick = function () {
                var content = $(‘textarea[sign = ‘+sign+‘]‘);
                content.val(content.val() + ‘[‘ + $(this).attr(‘title‘) + ‘]‘);
                $(‘#phiz‘).hide();
                }
            }
          }
      }
      //关闭表情框
    $(‘.close‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-100px -200px‘);
    }, function () {
        $(this).css(‘backgroundPosition‘, ‘-75px -200px‘);
    }).click(function () {
        $(this).parent().parent().hide();
        $(‘#phiz‘).hide();
        if ($(‘#turn‘).css(‘display‘) == ‘none‘) {
            $(‘#opacity_bg‘).remove();
        };
    });

});

/**
 * 统计字数
 * @param  字符串
 * @return 数组[当前字数, 最大字数]
 */
function check (str) {
    var num = [0, 140];
    for (var i=0; i<str.length; i++) {
        //字符串不是中文时
        if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 255){
            num[0] = num[0] + 0.5;//当前字数增加0.5个
            num[1] = num[1] + 0.5;//最大输入字数增加0.5个
        } else {//字符串是中文时
            num[0]++;//当前字数增加1个
        }
    }
    return num;
}

/**
 * 替换微博内容,去除 <a> 链接与表情图片
 */
function replace_weibo (content) {
    content = content.replace(/<img.*?title=[‘"](.*?)[‘"].*?\/?>/ig, ‘[$1]‘);
    content = content.replace(/<a.*?>(.*?)<\/a>/ig, ‘$1‘);
    return content.replace(/<span.*?>\&nbsp;(\/\/)\&nbsp;<\/span>/ig, ‘$1‘);
}
$(function () {

    //修改资料选项卡
    $(‘#sel-edit li‘).click( function () {
        var index = $(this).index();
        $(this).addClass(‘edit-cur‘).siblings().removeClass(‘edit-cur‘);
        $(‘.form‘).hide().eq(index).show();
    } );

    //城市联动
    var province = ‘‘;
    $.each(city, function (i, k) {
        province += ‘<option value="‘ + k.name + ‘" index="‘ + i + ‘">‘ + k.name + ‘</option>‘;
    });
    $(‘select[name=province]‘).append(province).change(function () {
        var option = ‘‘;
        if ($(this).val() == ‘‘) {
            option += ‘<option value="">请选择</option>‘;
        } else {
            var index = $(‘:selected‘, this).attr(‘index‘);
            var data = city[index].child;
            for (var i = 0; i < data.length; i++) {
                option += ‘<option value="‘ + data[i] + ‘">‘ + data[i] + ‘</option>‘;
            }
        }

        $(‘select[name=city]‘).html(option);
    });

    //所在地默认选项
    address = address.split(‘ ‘);
    $(‘select[name=province]‘).val(address[0]);
    $.each(city, function (i, k) {
        if (k.name == address[0]) {
            var str = ‘‘;
            for (var j in k.child) {
                str += ‘<option value="‘ + k.child[j] + ‘" ‘;
                if (k.child[j] == address[1]) {
                    str += ‘selected="selected"‘;
                }
                str += ‘>‘ + k.child[j] + ‘</option>‘;
            }
            $(‘select[name=city]‘).html(str);
        }
    });

    //星座默认选项
    $(‘select[name=night]‘).val(constellation);

    //头像上传 Uploadify 插件
    $(‘#face‘).uploadify({
        swf : PUBLIC + ‘/Uploadify/uploadify.swf‘,    //引入Uploadify核心Flash文件
        uploader : uploadUrl,    //PHP处理脚本地址
        width : 120,    //上传按钮宽度
        height : 30,    //上传按钮高度
        buttonImage : PUBLIC + ‘/Uploadify/browse-btn.png‘,    //上传按钮背景图地址
        fileTypeDesc : ‘Image File‘,    //选择文件提示文字
        fileTypeExts : ‘*.jpeg; *.jpg; *.png; *.gif‘,    //允许选择的文件类型
        formData : {‘session_id‘ : sid},
        //上传成功后的回调函数
        onUploadSuccess : function (file, data, response) {
            eval(‘var data = ‘ + data);
            if (data.status) {
                $(‘#face-img‘).attr(‘src‘, ROOT + ‘/Uploads/Face/‘ + data.path.max);
                $(‘input[name=face180]‘).val(data.path.max);
                $(‘input[name=face80]‘).val(data.path.medium);
                $(‘input[name=face50]‘).val(data.path.mini);
            } else {
                alert(data.msg);
            }
        }
    });

    //jQuery Validate 表单验证

    /**
     * 添加验证方法
     * 以字母开头,5-17 字母、数字、下划线"_"
     */
    jQuery.validator.addMethod("user", function(value, element) {
        var tel = /^[a-zA-Z][\w]{4,16}$/;
        return this.optional(element) || (tel.test(value));
    }, "以字母开头,5-17 字母、数字、下划线‘_‘");

    $(‘form[name=editPwd]‘).validate({
        errorElement : ‘span‘,
        success : function (label) {
            label.addClass(‘success‘);
        },
        rules : {
            old : {
                required : true,
                user : true
            },
            new : {
                required : true,
                user : true
            },
            newed : {
                required : true,
                equalTo : "#new"
            }
        },
        messages : {
            old : {
                required : ‘请填写旧密码‘,
            },
            new : {
                required : ‘请设置新密码‘
            },
            newed : {
                required : ‘请确认密码‘,
                equalTo : ‘两次密码不一致‘
            }
        }
    });
});
$(function () {

    $(‘.c-reply‘).toggle (function () {
        var name = $(this).parents(‘dl‘).find(‘dd a‘).html();
        var str = ‘回复@‘ + name.replace(/:$/, ‘ ‘) + ‘:‘;
        $(this).parent().next().show().find(‘textarea‘).val(str);
    }, function () {
        $(this).parent().next().hide();
    });

    //回复按钮
    $(‘.comment_btn‘).click(function () {
        var data = {
            wid : $(this).attr(‘wid‘),
            content : $(this).parents(‘ul‘).prev().val()
        };
        var obj = $(this).parents(‘.comment_list‘);

        $.post(replyUrl, data, function (data) {
            if (data) {
                alert(‘评论已回复‘);
                obj.hide();
            } else {
                alert(‘回复失败请重试...‘);
            }
        }, ‘json‘);
    });

    //删除评论
    $(‘.del-comment‘).click(function () {
        var data = {
            cid : $(this).attr(‘cid‘),
            wid : $(this).attr(‘wid‘)
        };
        var isDel = confirm(‘确定删除该评论?‘);
        var obj = $(this).parents(‘dl‘);

        if (isDel) {
            $.post(delComment, data, function (data) {
                if (data) {
                    obj.slideUp(‘slow‘, function () {
                        obj.remove();
                    });
                } else {
                    alert(‘删除失败请重试...‘);
                }
            }, ‘json‘);
        }
    });

    /**
     * 表情处理
     * 以原生JS添加点击事件,不走jQuery队列事件机制
     */
      var phiz = $(‘.phiz‘);
      for (var i = 0; i < phiz.length; i++) {
          phiz[i].onclick = function () {
              //定位表情框到对应位置
            $(‘#phiz‘).show().css({
                ‘left‘ : $(this).offset().left,
                ‘top‘ : $(this).offset().top + $(this).height() + 5
            });
            //为每个表情图片添加事件
            var phizImg = $("#phiz img");
            var sign = this.getAttribute(‘sign‘);
            for (var i = 0; i < phizImg.length; i++){
                phizImg[i].onclick = function () {
                var content = $(‘textarea[sign = ‘+sign+‘]‘);
                content.val(content.val() + ‘[‘ + $(this).attr(‘title‘) + ‘]‘);
                $(‘#phiz‘).hide();
                }
            }
          }
      }
      //关闭表情框
    $(‘.close‘).hover(function () {
        $(this).css(‘backgroundPosition‘, ‘-100px -200px‘);
    }, function () {
        $(this).css(‘backgroundPosition‘, ‘-75px -200px‘);
    }).click(function () {
        $(this).parent().parent().hide();
        $(‘#phiz‘).hide();
        if ($(‘#turn‘).css(‘display‘) == ‘none‘) {
            $(‘#opacity_bg‘).remove();
        };
    });
});
时间: 2025-01-04 08:22:50

常用 ajax js 表单的相关文章

JS表单验证-12个常用的JS表单验证

最近有个项目用到了表单验证,小编在项目完结后的这段时间把常用的JS表单验证demo整理了一下,和大家一起分享~~~ 1. 长度限制 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 6

12个常用的JS表单验证

长度限制 <form name="a" onsubmit="return test()"> <textarea name="b" cols="40" rows="6" placeholder="不能超过50个字符!"></textarea> <br /> <input type="submit" name=&quo

整理的一些常用的js表单验证

/** * 验证时间 * @param dataValue 格式为:YYYY-MM-DD * @returns 匹配返回true 不匹配返回false */ function valiDate(dateValue){ var result = dateValue.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3})

js表单验证工具包

常用的js表单验证方法大全 1 /* 2 非空校验 : isNull() 3 是否是数字: isNumber(field) 4 trim函数: trim() lTrim() rTrim() 5 校验字符串是否为空: checkIsNotEmpty(str) 6 校验字符串是否为整型: checkIsInteger(str) 7 校验整型最小值: checkIntegerMinValue(str,val) 8 校验整型最大值: checkIntegerMaxValue(str,val) 9 校验整

ajax form表单提交 input file中的文件

ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了解决这个问题我走了不少弯路: 1.用原生的 input file , 不支持ajax上传文件,你肯定会说可以用 ajax form表单上传了呀?不过我后面还要调用上传成功后用js处理一些对话框,所以这种方法排除 2.用了 uploadify 上传插件,弄出来能上传东西,结果不理想:因为不能判断上传的

jquery.validation.js 表单验证

jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 一导入js库 <script src="../js/jquery.js" type="text/javascript"></script> <script src="../js/jq

Jquery.validate.js表单验证插件的使用

作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例讲解应用.it's perfect. 首先记录一些使用过程中,爱犯的错误: 1>忘记给表单form添加id属性 2>input这些表单标签必须id属性和name属性名字一样.例如:<input type="text" id="name" name=&q

JS表单验证类HTML代码实例

以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮点数验证.日期验证.邮件检查.网址验证.固定电话和手机号码验证.IP地址验证.邮编和QQ号码验证. MSN和身份证验证等. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

ajax提交表单

ajax提交表单在项目中常用,前台无论是简单的html.jsp或者是使用了easyui框架,提交表单都会使用到ajax,extjs框架其实也是使用了ajax只不过对其进行了封装了,我们使用的时候就更固定了些. 总的来说ajax提交表单可以分为两种,一种是无返回结果的,就是将表单数据提交给后台,后台处理完就完了:另一种就是有返回结果的,后台执行成功或失败的信息需要返回到前台. 1,无返回结果的 最简单的就是$("#formid").submit();直接将form表单提交到后台. 2,有