javascript動態增加刪除修改條目執行個體代碼

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<style type="text/css">* {margin: 0;padding: 0;}body {font-family: 微軟雅黑;font-size: 14px;line-height: 24px;}.wrap {width: 960px;margin: 0 auto;}.wrap h2 {padding: 10px;text-align: center;}#list {padding-top: 10px;}#list dl {position: relative;top: 0;left: 0;border-top: #993 1px dashed;}#list dl dt {position: absolute;right: 0;width: 130px;padding: 0 10px;text-align: right;}#list dl dt a {margin-right: 10px;line-height: 40px;color: #333;text-decoration: none;}#list dl dt a:hover {color: #990;}#list dl dd {height: 40px;line-height: 40px;overflow: hidden;margin-right: 150px;padding: 0 10px;}#list dl dd input {width: 250px;height: 30px;line-height: 30px;font-family: 微軟雅黑;font-size: 14px;}.btnAdd {border-top: #990 1px solid;padding-top: 10px;}.btnAdd button {width: 120px;height: 40px;line-height: 40px;background-color: #993;border: #993 1px solid;border-radius: 8px;-webkit-border-radius: 8px;-moz-border-radius: 8px;box-shadow: inset 1px 1px 1px #fff, inset -1px -1px 1px #fff;-moz-box-shadow: inset 1px 1px 1px #fff, inset -1px -1px 1px #fff;-o-box-shadow: inset 1px 1px 1px #fff, inset -1px -1px 1px #fff;-ms-box-shadow: inset 1px 1px 1px #fff, inset -1px -1px 1px #fff;-webkit-box-shadow: inset 1px 1px 1px #fff, inset -1px -1px 1px #fff;}</style><div class="wrap"> <div class="eg"> < <h2>商品資訊管理</h2> <div id="list"> <dl> <dt>修改刪除</dt><dd>untitle</dd> </dl> </div> <div class="btnAdd"> <button id="addRecord">增加條目</button> </div> </div></div><script type="text/javascript">var addRecord = document.getElementById("addRecord");addRecord.onclick=function(){var list = document.getElementById("list");var newDl = document.createElement("dl");var newDt = document.createElement("dt");var newDd = document.createElement("dd");var newA = document.createElement("a");var newA1 = document.createElement("a");newA.setAttribute('href','');newA.setAttribute('onclick','return editItem(this)');newA1.setAttribute('href','');newA1.setAttribute('onclick','return deleteItem(this)');var txtEdit = document.createTextNode("修改");var txtDel = document.createTextNode("刪除");newA.appendChild(txtEdit);newA1.appendChild(txtDel);newDt.appendChild(newA);newDt.appendChild(newA1);newDd.appendChild(document.createTextNode("untitle"));newDl.appendChild(newDt);newDl.appendChild(newDd);list.appendChild(newDl);}function editItem(obj){var objParent = obj.parentNode;var objDd = objParent.nextSibling;var textDd = objDd.innerHTML;//alert(objDd.nodeName);objDd.innerHTML ="<input type='text' value='" + textDd + "' />";var inputbox = objDd.getElementsByTagName('input');inputbox[0].focus();obj.innerHTML = "儲存";obj.setAttribute('href','');obj.setAttribute('onclick','return saveItem(this)');return false;}function saveItem(obj){var objParent = obj.parentNode;var objDd = objParent.nextSibling;var inputbox = objDd.getElementsByTagName('input');var txtTitle = inputbox[0].value;objDd.innerHTML = txtTitle;obj.innerHTML = '修改';obj.setAttribute('onclick','return editItem(this)');return false;}function deleteItem(obj){var elm = obj.parentNode.parentNode;var list = document.getElementById('list');list.removeChild(elm);return false;}</script> </td> </tr></table>
提示:您可以先修改部分代碼再運行
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.