utils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * 让所有字符串支持空白过滤功能:trim
  3. * @retrn {String} 返回两端无空白的字符串
  4. */
  5. String.prototype.trim = function(){
  6. return this.replace(/^\s*|\s*$/g,"");
  7. };
  8. /**
  9. * 日期格式化
  10. * @param {Object} pattern
  11. */
  12. Date.prototype.format = function(pattern){
  13. var pad = function (source, length) {
  14. var pre = "",
  15. negative = (source < 0),
  16. string = String(Math.abs(source));
  17. if (string.length < length) {
  18. pre = (new Array(length - string.length + 1)).join('0');
  19. }
  20. return (negative ? "-" : "") + pre + string;
  21. };
  22. if ('string' != typeof pattern) {
  23. return this.toString();
  24. }
  25. var replacer = function(patternPart, result) {
  26. pattern = pattern.replace(patternPart, result);
  27. }
  28. var year = this.getFullYear(),
  29. month = this.getMonth() + 1,
  30. date2 = this.getDate(),
  31. hours = this.getHours(),
  32. minutes = this.getMinutes(),
  33. seconds = this.getSeconds();
  34. replacer(/yyyy/g, pad(year, 4));
  35. replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2));
  36. replacer(/MM/g, pad(month, 2));
  37. replacer(/M/g, month);
  38. replacer(/dd/g, pad(date2, 2));
  39. replacer(/d/g, date2);
  40. replacer(/HH/g, pad(hours, 2));
  41. replacer(/H/g, hours);
  42. replacer(/hh/g, pad(hours % 12, 2));
  43. replacer(/h/g, hours % 12);
  44. replacer(/mm/g, pad(minutes, 2));
  45. replacer(/m/g, minutes);
  46. replacer(/ss/g, pad(seconds, 2));
  47. replacer(/s/g, seconds);
  48. return pattern;
  49. };
  50. /**
  51. * 自动消失的Alert弹窗
  52. * @param content
  53. */
  54. window.alert = function (content) {
  55. window.clearTimeout(window.feHelperAlertMsgTid);
  56. var elAlertMsg = $("#fehelper_alertmsg").hide();
  57. if(!elAlertMsg.get(0)) {
  58. elAlertMsg = $('<div id="fehelper_alertmsg" style="position:fixed;top:5px;right:5px;z-index:1000000">' +
  59. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  60. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>').appendTo('body');
  61. }else{
  62. elAlertMsg.find('p').text(content).end().show();
  63. }
  64. window.feHelperAlertMsgTid = window.setTimeout(function () {
  65. elAlertMsg.hide(100);
  66. }, 3000);
  67. };