Loading... JS代码: ```js function update(id,html){ //编辑后焦点离开文本框时执行的方法 alert("id:"+id+";新内容:"+html); } function ShowElement(id) { //双击使文本可编辑 var oldhtml = document.getElementById(id).innerHTML; var newobj = document.createElement('textarea'); newobj.type = 'text'; newobj.value = oldhtml; newobj.onblur = function() { document.getElementById(id).innerHTML = this.value == oldhtml ? oldhtml : this.value; update(id,document.getElementById(id).innerHTML); } document.getElementById(id).innerHTML = ''; document.getElementById(id).appendChild(newobj); newobj.setSelectionRange(0, oldhtml.length); newobj.focus(); } ``` 调用时直接在希望双击可编辑的文本标签添加id="xxx" ondblclick="ShowElement(xxx)"即可。 希望单击时可编辑只需将ondblclick改为onclick即可。 最后修改:2021 年 11 月 23 日 © 允许规范转载 打赏 赞赏作者 微信 赞 0 如果觉得我的文章对你有用,请随意赞赏