不注释,不解释
- <html>
- <head>
- <script src="mootools.js" ></script>
- <script>
- var Editor=new Class({
- initialize: function(){
- this.elements = {};
- this.error=[];
- this.events=new Events();
- },
- getError:function (){
- var error=this.error;
- this.error=[];
- return JSON.encode(error);
- }
- ,
- //编辑对象,开始; 需要属性 group=44 name="password" [type="password"]
- toEdit:function (obj){
- var att=obj.attributes;
- var name=att['name'].value;
- var type=att['type']?att['type'].value:'text';
- if(!att['group']){
- this.error.push('unknow group');
- return;
- }
- var group = att['group'].value;
- this.elements[group]=this.elements[group]||{};
- var ele=this.elements[group];
- if(!ele[name])
- {
- value=obj.innerHTML;
- obj.innerHTML="<input type='"+type+"' style='width:"+(obj.offsetWidth+10)+"px;' name="+name+" />";
- $(obj).getElement('input').value=value;
- ele[name]={object:obj};
- this.events.fireEvent("toEdit",{type:'toEdit',group:group,object:obj,name:name});
- }
- },
- getValue:function(group,name){
- var element=this.elements[group];
- if(element[name]){
- var object=element[name].object;
- var input=$(object).getElement('input');
- var value=input.value;
- return value;
- }
- return null;
- },
- foreach:function(group,fun){
- var element=this.elements[group];
- for(var name in element){
- if(element[name]){
- fun({group:group,name:name,element:element,object:element[name].object,value:this.getValue(group,name)});
- }
- }
- }
- ,
- //编辑结束,获得所有修改,返回json,key为name属性
- endEdit:function (obj,group,ext_data){
- var content={};
- for(var e in ext_data){
- content[e]=ext_data[e];
- }
- this.foreach(group,function(data){
- var object=data.object;
- var check=object.attributes["check"];
- if(check){
- alert(eval(check.value));
- if(!(eval(check.value)(data.value)))
- return false;
- }
- });
- this.foreach(group,function(data){
- var object=data.object;
- content[data.name]=data.value.replace(/'/g,"\\'");
- object.innerHTML=data.value;
- data.element[data.name]=null;
- });
- this.events.fireEvent("endEdit",{type:'endEdit',object:obj,group:group});
- return JSON.encode(content);
- }
- });
- var editor=new Editor();
- editor.events.addEvent("toEdit",editStart);
- editor.events.addEvent("endEdit",editEnd);
- function check_num(value){
- return value.length<10;
- }
- function editStart(data){
- //alert(data.group);
- $("button"+data.group).style.display="";
- }
- function editEnd(data){
- //alert(data.group);
- $("button"+data.group).style.display="none";
- }
- function preview()
- {
- bdhtml=window.document.body.innerHTML;
- sprnstr="<!--startprint-->";
- eprnstr="<!--endprint-->";
- prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
- prnhtmlprnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
- window.document.body.innerHTML=prnhtml;
- window.print();
- }
- </script>
- </head>
- <body>
- <table border='1'>
- <tr>
- <td onclick="editor.toEdit(this);" group=44 check="check_num" name="name">aaa</td>
- <td onclick="editor.toEdit(this);" group=44 name="password" type="password">123</td>
- <td onclick="editor.toEdit(this);" group=44 name="id">1222</td>
- <td><span onclick="editor.endEdit(this);" group=44 name="span">s</span></td>
- <td><input id="group44" type='button' onclick="$('message').innerHTML=editor.endEdit(44,{group_id:44}); $('error').innerHTML=editor.getError();" value="ok"/></td>
- <td id='message'></td>
- <td id='error'></td>
- </tr>
- <!--startprint-->
- <tr>
- <td onclick="editor.toEdit(this);" group=55 check="check_num" name="name">aaa</td>
- <td onclick="editor.toEdit(this);" group=55 name="password" type="password">123</td>
- <td onclick="editor.toEdit(this);" group=55 name="id">1222</td>
- <td onclick="editor.toEdit(this);" name="id">1222</td>
- <td><span onclick="editor.toEdit(this);" group=55 name="span">s</span></td>
- <td><input id="button55" type='button' style="display:none" onclick="$('message').innerHTML=editor.endEdit(this,55,{group_id:55}); $('error').innerHTML=editor.getError();" value="ok"/></td>
- <td id='message'></td>
- <td id='error'></td>
- </tr>
- <!--endprint-->
- <tr>
- <td><input type="button" onclick="preview()" />
- </td>
- </tr>
- </table>
- </body>
- </html>