post.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var len = function (str) {
  2. if (!str)
  3. return 0;
  4. var length = 0;
  5. for (var i = 0; i < str.length; i++) {
  6. if (str.charCodeAt(i) >= 0x4e00 && str.charCodeAt(i) <= 0x9fa5) {
  7. length += 2;
  8. } else {
  9. length++;
  10. }
  11. }
  12. return length;
  13. };
  14. var ci, si;
  15. $(function () {
  16. $userinfo = localStorage.getItem("commentuserinfo");
  17. $userinfo = JSON.parse($userinfo);
  18. if ($userinfo && typeof $userinfo.username !== 'undefined') {
  19. $("#username").val($userinfo.username);
  20. $("#email").val($userinfo.email);
  21. $("#website").val($userinfo.website);
  22. }
  23. var loadcomment = function (page) {
  24. $("#respond h3 a").trigger("click");
  25. $.ajax({
  26. url: getcommentlist_url,
  27. data: {post_id: post_id, page: page},
  28. type: 'GET',
  29. success: function (data) {
  30. $(".commentlist").html(data);
  31. $('html, body').animate({scrollTop: $('#comments').position().top}, 'slow');
  32. }, error: function () {
  33. }
  34. });
  35. };
  36. $(document).on("click", ".pager a", function () {
  37. var page = $(this).text();
  38. loadcomment(page);
  39. return false;
  40. });
  41. $(document).on("click", "#submit", function () {
  42. var rememberme = $("input[name=rememberme]:checked").val() ? 1 : 0;
  43. var btn = $(this);
  44. var tips = $("#actiontips");
  45. tips.removeClass();
  46. var username = $("#username").val();
  47. var email = $("#email").val();
  48. var website = $("#website").val();
  49. var content = $("#commentcontent").val();
  50. if (len(username) < 3 || len(email) < 3 || len(content) < 3) {
  51. tips.addClass("error").html("姓名、Email、评论内容长度不正确!最少3个字符").fadeIn().change();
  52. return false;
  53. }
  54. btn.attr("disabled", "disabled");
  55. tips.html('正在提交...');
  56. $.ajax({
  57. url: postcomment_url,
  58. type: 'POST',
  59. data: $("#postform").serialize(),
  60. dataType: 'json',
  61. success: function (json) {
  62. btn.removeAttr("disabled");
  63. if (json.code == 1) {
  64. if (rememberme) {
  65. localStorage.setItem("commentuserinfo", JSON.stringify({username: username, email: email, website: website}));
  66. } else {
  67. localStorage.removeItem("commentuserinfo");
  68. }
  69. $("#pid").val(0);
  70. tips.addClass("success").html(json.msg).fadeIn(300).change();
  71. $("#commentcontent").val('');
  72. loadcomment(1);
  73. $("#commentcount").text(parseInt($("#commentcount").text()) + 1);
  74. } else {
  75. tips.addClass("error").html(json.msg).fadeIn().change();
  76. }
  77. if (json.data && json.data.token) {
  78. $("#postform input[name='__token__']").val(json.data.token);
  79. }
  80. },
  81. error: function () {
  82. btn.removeAttr("disabled");
  83. tips.addClass("error").html("评论失败!请刷新页面重试!").fadeIn();
  84. }
  85. });
  86. return false;
  87. });
  88. $("#commentcontent").on("keydown", function (e) {
  89. if (e.ctrlKey && e.which == 13) {
  90. $("#submit").click();
  91. }
  92. });
  93. $("#actiontips").on("change", function () {
  94. clearTimeout(si);
  95. si = setTimeout(function () {
  96. $("#actiontips").fadeOut();
  97. }, 8000);
  98. });
  99. $(document).on("keyup change", "#commentcontent", function () {
  100. var max = 1000;
  101. var c = $(this).val();
  102. var length = len(c);
  103. var t = $("#actiontips");
  104. if (max >= length) {
  105. t.removeClass().show().addClass("loading").html("你还可以输入 <font color=green>" + (Math.floor((max - length) / 2)) + "</font> 字");
  106. $("#submit").removeAttr("disabled");
  107. } else {
  108. t.removeClass().show().addClass("loading").html("你已经超出 <font color=red>" + (Math.ceil((length - max) / 2)) + "</font> 字");
  109. $("#submit").attr("disabled", "disabled");
  110. }
  111. });
  112. $(".commentlist dl dd div,.commentlist dl dd dl dd").on({
  113. mouseenter: function () {
  114. clearTimeout(ci);
  115. var _this = this;
  116. ci = setTimeout(function () {
  117. $(_this).find("small:first").find("a").stop(true, true).fadeIn();
  118. }, 300);
  119. },
  120. mouseleave: function () {
  121. $(this).find("small:first").find("a").stop(true, true).fadeOut();
  122. }
  123. });
  124. $(document).on("click", ".reply", function () {
  125. $("#pid").val($(this).attr("rel"));
  126. $(this).parent().parent().append($("div#respond").detach());
  127. $("#respond h3 a").show();
  128. $("#commentcontent").focus().val($(this).attr("title"));
  129. });
  130. $(document).on("click", "#respond h3 a", function () {
  131. $(".commentlist").after($("div#respond").detach());
  132. $(this).hide();
  133. });
  134. $(document).on("click", ".expandall a", function () {
  135. $(this).parent().parent().find("dl.hide").fadeIn();
  136. $(this).fadeOut();
  137. });
  138. //超过指定宽度
  139. var nc = $(".entry");
  140. if (nc.size() > 0) {
  141. var nw = nc.width();
  142. $("img", nc).each(function (i, obj) {
  143. var iw = $(obj).removeAttr("height").width();
  144. if (iw > nw) {
  145. $(obj).width(nw).css("cursor", "pointer").attr("title", "点击查看大图片").bind('click', function () {
  146. window.open($(obj).attr("src"));
  147. });
  148. }
  149. });
  150. }
  151. });