常用 ajax js 表单

2023-05-04,,

$(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') : ;
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()) / ;
var turnTop = $(document).scrollTop() + ($(window).height() - $('#turn').height()) / ;
$('#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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
var num = - Math.ceil(lengths[]);
var msg = num < ? : 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 == ) {
msg = '收藏成功';
} if (data == -) {
msg = '已收藏';
} if (data == ) {
msg = '收藏失败';
} keepUp.html(msg).fadeIn();
setTimeout(function () {
keepUp.fadeOut();
}, ); }, '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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
});
//回复
$('.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() ? :
}; $.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 = ; i < phiz.length; i++) {
phiz[i].onclick = function () {
//定位表情框到对应位置
$('#phiz').show().css({
'left' : $(this).offset().left,
'top' : $(this).offset().top + $(this).height() +
});
//为每个表情图片添加事件
var phizImg = $("#phiz img");
var sign = this.getAttribute('sign');
for (var i = ; 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 = [, ];
for (var i=; i<str.length; i++) {
//字符串不是中文时
if (str.charCodeAt(i) >= && str.charCodeAt(i) <= ){
num[] = num[] + 0.5;//当前字数增加0.5个
num[] = num[] + 0.5;//最大输入字数增加0.5个
} else {//字符串是中文时
num[]++;//当前字数增加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') : ;
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()) / ;
var turnTop = $(document).scrollTop() + ($(window).height() - $('#turn').height()) / ;
$('#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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
var num = - Math.ceil(lengths[]);
var msg = num < ? : 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 == ) {
msg = '收藏成功';
} if (data == -) {
msg = '已收藏';
} if (data == ) {
msg = '收藏失败';
} keepUp.html(msg).fadeIn();
setTimeout(function () {
keepUp.fadeOut();
}, ); }, '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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
});
//回复
$('.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() ? :
}; $.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 = ; i < phiz.length; i++) {
phiz[i].onclick = function () {
//定位表情框到对应位置
$('#phiz').show().css({
'left' : $(this).offset().left,
'top' : $(this).offset().top + $(this).height() +
});
//为每个表情图片添加事件
var phizImg = $("#phiz img");
var sign = this.getAttribute('sign');
for (var i = ; 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 = [, ];
for (var i=; i<str.length; i++) {
//字符串不是中文时
if (str.charCodeAt(i) >= && str.charCodeAt(i) <= ){
num[] = num[] + 0.5;//当前字数增加0.5个
num[] = num[] + 0.5;//最大输入字数增加0.5个
} else {//字符串是中文时
num[]++;//当前字数增加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]{,}$/;
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 : [,],
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()) / ) + 'px',
'top' : ($(window).height() - ($(window).height() / )) + + 'px'
//添加点击事件
}).click(function() {
$("html, body").animate({scrollTop: }, );
});
//添加窗口滚动事件
$(window).scroll(function () {
var st = $(document).scrollTop();
//IE6定位
if (window.ActiveXObject&&!window.XMLHttpRequest) {
var ieTop = st + ($(window).height() / + );
$('.backToTop').css('top', ieTop + 'px');
}
//滚动条高度大于100时显示 返回顶部按钮
(st > ) ? $('.backToTop').show() : $('.backToTop').hide();
}); //创建好友分组
$('#create_group').click(function () {
var groupLeft = ($(window).width() - $('#add-group').width()) / ;
var groupTop = $(document).scrollTop() + ($(window).height() - $('#add-group').height()) / ;
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()) / ;
var followTop = $(document).scrollTop() + ($(window).height() - $('#follow').height()) / ;
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()) / ;
var modelTop = $(document).scrollTop() + ($(window).height() - $('#model').height()) / ;
$('#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);
}, );
});
} /**
* 推送的新消息
* @param {[type]} json {total:新消息的条数,type:(1:评论,2:私信,3:@我)}
* @return {[type]} [description]
*/
var flags = true;
function news (json) {
switch (json.type) {
case :
$('#news ul .news_comment').show().find('a').html(json.total + '条新评论');
break;
case :
$('#news ul .news_letter').show().find('a').html(json.total + '条新私信');
break;
case :
$('#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");
}, );
}
} /**
* 创建全屏透明背景层
* @param id
*/
function createBg (id) {
$('<div id = "' + id + '"></div>').appendTo('body').css({
'width' : $(document).width(),
'height' : $(document).height(),
'position' : 'absolute',
'top' : ,
'left' : ,
'z-index' : ,
'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 < ? : OX - DX > DW - OW ? DW - OW : OX - DX;
top = OY - DY < ? : 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 : ;
var tipsDiv = '<div class="tipsClass">' + tips + '</div>';
$( 'body' ).append( tipsDiv );
$( 'div.tipsClass' ).css({
'top' : height/ + 'px',
'left' : ( windowWidth / ) - + 'px',
'position' : 'absolute',
'padding' : '3px 5px',
'background': '#670768',
'font-size' : + '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/-+'px'
}, "slow").fadeOut();
}, time * );
}
$(function () {
//jQuery Validate 表单验证 /**
* 添加验证方法
* 以字母开头,5-17 字母、数字、下划线"_"
*/
jQuery.validator.addMethod("user", function(value, element) {
var tel = /^[a-zA-Z][\w]{,}$/;
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()) / ;
var letterTop = $(document).scrollTop() + ($(window).height() - $('#letter').height()) / ;
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 : , //上传按钮宽度
height : , //上传按钮高度
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[] > ) {//当前有输入内容时改变发布按钮背景
$('.send_btn').css('backgroundPosition', '-133px -50px');
} else {//内容为空时发布按钮背景归位
$('.send_btn').css('backgroundPosition', '-50px -50px');
};
//最大允许输入140字个
if (lengths[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
var num = - Math.ceil(lengths[]);
var msg = num < ? : 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 = ;
var glint = setInterval(function () {
if (timeOut % == ) {
cons.css('background','#FFA0C2');
} else {
cons.css('background','#fff');
}
timeOut++;
if (timeOut > ) {
clearInterval(glint);
cons.focus();
}
}, );
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') : ;
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()) / ;
var turnTop = $(document).scrollTop() + ($(window).height() - $('#turn').height()) / ;
$('#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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
var num = - Math.ceil(lengths[]);
var msg = num < ? : 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 == ) {
msg = '收藏成功';
} if (data == -) {
msg = '已收藏';
} if (data == ) {
msg = '收藏失败';
} keepUp.html(msg).fadeIn();
setTimeout(function () {
keepUp.fadeOut();
}, ); }, '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[] >= ) {
$(this).val(content.substring(, Math.ceil(lengths[])));
}
});
//回复
$('.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() ? :
}; $.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 = ; i < phiz.length; i++) {
phiz[i].onclick = function () {
//定位表情框到对应位置
$('#phiz').show().css({
'left' : $(this).offset().left,
'top' : $(this).offset().top + $(this).height() +
});
//为每个表情图片添加事件
var phizImg = $("#phiz img");
var sign = this.getAttribute('sign');
for (var i = ; 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 = [, ];
for (var i=; i<str.length; i++) {
//字符串不是中文时
if (str.charCodeAt(i) >= && str.charCodeAt(i) <= ){
num[] = num[] + 0.5;//当前字数增加0.5个
num[] = num[] + 0.5;//最大输入字数增加0.5个
} else {//字符串是中文时
num[]++;//当前字数增加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 = ; 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[]);
$.each(city, function (i, k) {
if (k.name == address[]) {
var str = '';
for (var j in k.child) {
str += '<option value="' + k.child[j] + '" ';
if (k.child[j] == address[]) {
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 : , //上传按钮宽度
height : , //上传按钮高度
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]{,}$/;
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 = ; i < phiz.length; i++) {
phiz[i].onclick = function () {
//定位表情框到对应位置
$('#phiz').show().css({
'left' : $(this).offset().left,
'top' : $(this).offset().top + $(this).height() +
});
//为每个表情图片添加事件
var phizImg = $("#phiz img");
var sign = this.getAttribute('sign');
for (var i = ; 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();
};
});
});

常用 ajax js 表单的相关教程结束。

《常用 ajax js 表单.doc》

下载本文的Word格式文档,以方便收藏与打印。