jqueryeditable

make any html element editable inline

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/387585/717975/jqueryeditable.js

  1. (function($){
  2. var escape_html = function(str){
  3. return str.replace(/</gm, '&lt;').replace(/>/gm, '&gt;');
  4. };
  5. var unescape_html = function(str){
  6. return str.replace(/&lt;/gm, '<').replace(/&gt;/gm, '>');
  7. };
  8.  
  9. $.fn.editable = function(event, callback){
  10. if(typeof callback !== 'function') callback = function(){};
  11. if(typeof event === 'string'){
  12. var trigger = this;
  13. var action = event;
  14. var type = 'input';
  15. }
  16. else if(typeof event === 'object'){
  17. var trigger = event.trigger || this;
  18. if(typeof trigger === 'string') trigger = $(trigger);
  19. var action = event.action || 'click';
  20. var type = event.type || 'input';
  21. }
  22. else{
  23. throw('Argument Error - jQuery.editable("click", function(){ ~~ })');
  24. }
  25.  
  26. var target = this;
  27. var edit = {};
  28.  
  29. edit.start = function(e){
  30. trigger.unbind(action === 'clickhold' ? 'mousedown' : action);
  31. if(trigger !== target) trigger.hide();
  32. var old_value = (
  33. type === 'textarea' ?
  34. target.text().replace(/<br( \/)?>/gm, '\n').replace(/&gt;/gm, '>').replace(/&lt;/gm, '<') :
  35. target.text()
  36. ).replace(/^\s+/,'').replace(/\s+$/,'');
  37.  
  38. var input = type === 'textarea' ? $('<textarea>') : $('<input>');
  39. input.val(old_value).
  40. css('width', type === 'textarea' ? '100%' : target.width()+target.height() ).
  41. css('font-size','100%').
  42. css('margin',0).attr('id','editable_'+(new Date()*1)).
  43. addClass('editable');
  44. if(type === 'textarea') input.css('height', target.height());
  45.  
  46. var finish = function(){
  47. var result = input.val().replace(/^\s+/,'').replace(/\s+$/,'');
  48. var html = escape_html(result);
  49. if(type === 'textarea') html = html.replace(/[\r\n]/gm, '<br />');
  50. target.html(html);
  51. callback({value : result, target : target, old_value : old_value});
  52. edit.register();
  53. if(trigger !== target) trigger.show();
  54. };
  55.  
  56. input.blur(finish);
  57. if(type === 'input'){
  58. input.keydown(function(e){
  59. if(e.keyCode === 13) finish();
  60. });
  61. }
  62.  
  63. target.html(input);
  64. input.focus();
  65. };
  66.  
  67. edit.register = function(){
  68. if(action === 'clickhold'){
  69. var tid = null;
  70. trigger.bind('mousedown', function(e){
  71. tid = setTimeout(function(){
  72. edit.start(e);
  73. }, 500);
  74. });
  75. trigger.bind('mouseup mouseout', function(e){
  76. clearTimeout(tid);
  77. });
  78. }
  79. else{
  80. trigger.bind(action, edit.start);
  81. }
  82. };
  83. edit.register();
  84.  
  85. return this;
  86. };
  87. })(jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址