javascript實(shí)現(xiàn)節(jié)點(diǎn)(div)名稱編輯
節(jié)點(diǎn)html代碼如下:
<div class="img_1" id="img_1" >
<input type="image" class="img_1" src="img/cump.png"></input>
<div class="noteText" id="noteTxt" type="text" ondblclick ="changeName(this.id);">123</div>
</div>
js編輯noteTxt文本,function如下:
function changeName(noteTxtId){
var noteTxt = document.getElementById(noteTxtId);
noteTxt.style.display= "none";//.style.display= "block"
var div = noteTxt.parentNode;
if(!document.getElementById("noteInput")){
var text=document.createElement("input");
text.type="text";
text.id="noteInput";
text.style.width=getStyle(noteTxt,'width');
text.style.height=getStyle(noteTxt,'height');
text.style.marginTop=getStyle(noteTxt,'marginTop');
text.style.textAlign=getStyle(noteTxt,'textAlign');
text.value=noteTxt.innerHTML;
div.appendChild(text);
text.select();
text.onblur=function(){
noteTxt.style.display= "block";
noteTxt.innerHTML=text.value;
//text.style.display= "none";
div.removeChild(text);
}
}
}
//獲得css文件中的樣式
function getStyle(obj, attr)
{
if(obj.currentStyle)
{
return obj.currentStyle[attr]; //IE
}else{
return getComputedStyle(obj,false)[attr]; //FF
}
}
css如下:
.img_1 {
width: 80px;
height:70px;
position:absolute;
}
.noteText {
width:80px;
height:15px;
text-align:center;
margin-top:70px;
word-break:break-all;
}
代碼非常的簡(jiǎn)單,這里就不多做解釋了,下伙伴們可以自由發(fā)揮下,使用到自己的項(xiàng)目中去。
- JavaScript動(dòng)態(tài)添加style節(jié)點(diǎn)的方法
- javascript刪除元素節(jié)點(diǎn)removeChild()用法實(shí)例
- Javascript節(jié)點(diǎn)關(guān)系實(shí)例分析
- javascript動(dòng)態(tài)修改Li節(jié)點(diǎn)值的方法
- javascript使用appendChild追加節(jié)點(diǎn)實(shí)例
- 用javascript讀取xml文件讀取節(jié)點(diǎn)數(shù)據(jù)
- javascript 獲取HTML DOM父、子、臨近節(jié)點(diǎn)
- JavaScript實(shí)現(xiàn)獲得所有兄弟節(jié)點(diǎn)的方法
相關(guān)文章
JS實(shí)現(xiàn)點(diǎn)擊按鈕控制Div變寬、增高及調(diào)整背景色的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊按鈕控制Div變寬、增高及調(diào)整背景色的方法,涉及javascript動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,適用于動(dòng)態(tài)更換頁(yè)面皮膚的功能,需要的朋友可以參考下2015-08-08js實(shí)現(xiàn)界面向原生界面發(fā)消息并跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)界面向原生界面發(fā)消息并跳轉(zhuǎn)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11JS+CSS實(shí)現(xiàn)自動(dòng)切換的網(wǎng)頁(yè)滑動(dòng)門菜單效果代碼
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)自動(dòng)切換的網(wǎng)頁(yè)滑動(dòng)門菜單效果代碼,涉及JavaScript基于時(shí)間函數(shù)動(dòng)態(tài)變換頁(yè)面tab樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09一文帶你徹底搞懂JS大文件分片上傳的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了前端JavaScript是如何實(shí)現(xiàn)大文件分片上傳的,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-05-05js中用window.open()打開(kāi)多個(gè)窗口的name問(wèn)題
這篇文章主要介紹了js中用window.open()打開(kāi)多個(gè)窗口的問(wèn)題,需要的朋友可以參考下2014-03-03