使用dom元素和jquery元素實現簡單增刪改的練習

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   java   ar   for   

    軟體開發實際就是資料的增刪改查,javascript前端開發也不例外。今天學了jquery架構的簡單使用。於是用它實現簡單的增刪改,接著也用原始的javascript實現同樣的功能,以便看出jquery的強大:

代碼如下:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <script type="text/javascript" src="jq/jquery-1.9.1.js"></script>    <script type="text/javascript">        $(function(){            gaoliang();                        var $seldel = undefined;            var seldel = undefined;                    //高亮選中            function gaoliang() {                $("li").click(function () {                    $("li").css(                        "backgroundColor", "red"                        );                    this.style.backgroundColor = "yellow";                    $seldel = $(this);                                        seldel = this;                });            }            //使用jquery添加對象            $("#btnAdd1").click(function () {                var input = window.prompt("輸入資料");                var $newli = $("<li>" + input + "</li>");                $newli.appendTo("#Ol");                gaoliang();            });            //使用dom元素添加對象            document.getElementById("btnAdd2").onclick = function () {                var input = window.prompt("輸入資料");                                var newli = document.createElement("li");                newli.innerHTML = input;                document.getElementById("Ol").appendChild(newli);                gaoliang();            }            //使用jquery刪除對象            $("#btnDel1").click(function () {                $seldel.remove();                            });            //使用dom元素刪除對象            document.getElementById("btnDel2").onclick = function () {                seldel.parentNode.removeChild(seldel);            }            //使用jquery插入資料            $("#btnInsert1").click(function () {                var input=window.prompt("輸入插入的資料");                var $newli=$("<li>"+ input+"</li>");                $newli.insertBefore($seldel);                gaoliang();            });            //使用dom插入資料            document.getElementById("btnInsert2").onclick = function () {                var Ol = document.getElementById("Ol");                var input = window.prompt("輸入插入的資料");                var newli = document.createElement("li");                newli.innerHTML = input;                Ol.insertBefore(newli, seldel);                                                                gaoliang();                            }            //使用jquery編輯選中的資料            $("#btnEdit1").click(function () {                var oldtxt = $seldel.html();                var $edit = $("<input id=‘btnE‘ type=‘text‘ value=‘" + oldtxt + "‘/>");                $seldel.html($edit);                $edit.focus();                $edit.blur(function () {                    var newtxt = $(this).val();                    $seldel.html(newtxt);                });                                            });            //使用dom編輯選中的資料            document.getElementById("btnEdit2").onclick = function () {                var edittext = document.createElement("input");                edittext.type = "text";                edittext.value = seldel.innerHTML;;                seldel.innerHTML = "";                seldel.appendChild(edittext);                edittext.focus();                                            edittext.onblur = function () {                    seldel.innerHTML = edittext.value;                }                                          }                               } )    </script></head><body>    <ol id="Ol">        <li id="haha">1</li>         <li>2</li>         <li>3</li>         <li>4</li>    </ol>    <input id="btnAdd1" type="button" value="jquery添加資料" />     <input id="btnAdd2" type="button" value="dom添加資料" />     <input id="btnDel1" type="button" value="jquery刪除資料" />    <input id="btnDel2" type="button" value="dom刪除資料" />     <input id="btnInsert1" type="button" value="jquery插入資料" />     <input id="btnInsert2" type="button" value="dom插入資料" />    <input id="btnEdit1" type="button" value="jquery編輯資料" />     <input id="btnEdit2" type="button" value="dom編輯資料" /></body></html>

 

使用dom元素和jquery元素實現簡單增刪改的練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.