JavaScript 就地编辑HTML节点实现代码
作者:bea
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>点击编辑当前内容</title> &
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>点击编辑当前内容</title>
<script type="text/javascript">
var EditField={
configure:function(id){
this.id=id;
this.createElements(); //动态生成编辑输入框
this.toScan(); //初始化动态生成的输入框和按钮、待编辑的DOM元素的display属性
this.addEvents(); //给相关的DOM元素添加事件监听器
},
events:function(elem,type,fn){ //用于添加事件的通用函数
if(elem.attachEvent){
elem.attachEvent("on"+type,fn);
}else if(elem.addEventListener){
elem.addEventListener(type,fn,false);
}else{
elem["on"+type]=fn;
}
return elem;
},
addEvents:function(){ //添加事件
var that=this;
this.events(this.btnSubmit,"click",function(){
that.save();
});
this.events(this.btnCancel,"click",function(){
that.cancel();
});
this.events(document.getElementById(this.id),"click",function(){
that.toEdit();
});
},
insertAfter:function(newNode,referenceNode){ //将动态生成的输入框和按钮插入到待编辑元素的后面
if (referenceNode.nextSibling) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}else{
var p=referenceNode.parentNode || document.body;
p.appendChild(newNode);
}
},
createElements:function(){ //动态生成输入框和按钮
this.divContainer=document.createElement("div");
//this.parentElement.appendChild(this.divContainer);
this.insertAfter(this.divContainer,document.getElementById(this.id));
this.input=document.createElement("input");
this.input.type="text";
this.input.value=document.getElementById(this.id).innerHTML; //初始化值
this.divContainer.appendChild(this.input);
this.btnSubmit=document.createElement("input");
this.btnSubmit.value="Submit";
this.btnSubmit.type="button";
this.divContainer.appendChild(this.btnSubmit);
this.btnCancel=document.createElement("input");
this.btnCancel.type="button";
this.btnCancel.value="Cancel";
this.divContainer.appendChild(this.btnCancel);
},
toEdit:function(){ //转换成编辑状态
this.divContainer.style.display="block";
document.getElementById(this.id).style.display="none";
this.setValue();
},
toScan:function(){ //转换成浏览状态
document.getElementById(this.id).style.display="block";
this.divContainer.style.display="none";
},
getValue:function(){ //获取输入框的值
return this.input.value;
},
setValue:function(){ //设置输入框的值
this.input.value=document.getElementById(this.id).innerHTML;
},
save:function(){ //保存编辑结果
document.getElementById(this.id).innerHTML=this.getValue();
this.toScan();
},
cancel:function(){ //取消当前的编辑
this.toScan();
}
};
onload=function(){
EditField.configure("p"); //调用configure函数,确定那个DOM元素进行就地编辑
}
</script>
</head>
<body>
<h3>Edit Demo</h3>
<p id="p"> </p>
<p>Copyright:Super sha.</p>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 利用javascript/jquery对上传文件格式过滤的方法
- javascript 动态table添加colspan owspan 参数的方法
- jquery 表单下所有元素的隐藏
- 运用jquery实现table单双行不同显示并能单行选中
- 用JQuery 实现AJAX加载XML并解析的脚本
- Prototype Selector对象学习
- Prototype 工具函数 学习
- 用js模仿word格式刷功能实现代码 [推荐]
- JQuery CSS样式控制 学习笔记
- JQuery 学习笔记 element属性控制
- JQuery 学习笔记 选择器之六
- JQuery 学习笔记 选择器之五
- JQuery 学习笔记 选择器之四
- JQuery 学习笔记 选择器之三
- JQuery 学习笔记 选择器之二
- JQuery 学习笔记 选择器之一
- javascript IFrame 强制刷新代码
- jQuery TextBox自动完成条
- JavaScript 动态生成方法的例子