JQuery常見節(jié)點操作實例分析
本文實例講述了JQuery常見節(jié)點操作。分享給大家供大家參考,具體如下:
插入節(jié)點
append()
和appengTo()
:在現(xiàn)存元素內(nèi)部,從后面插入
prepend()
和prependTo()
:在現(xiàn)存元素外部,從前面插入
after()
和insertAfter()
:在現(xiàn)存元素外部,從后面插入
before()
和insertBefore()
:在現(xiàn)存元素外部,從前面插入
新建節(jié)點的插入
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { // $('#div1').html('')這種用字符串的方式塞進去的性能是最高的, // 但是有點時候不方便用,因為這樣會重寫div1里面的元素 $a=$('<a href="#" rel="external nofollow" >鏈接</a>>'); $('#div1').append($a);/*在最后加入字符串,append從現(xiàn)成的元素的后面插入元素*/ $a.appendTo($('#div1'));/*和append效果相同*/ $p=$('<p>這是一個p標(biāo)簽</p>'); $("#div1").prepend($p); $h2=$('<h2>這是一個h2</h2>'); $('#div1').after($h2); $h3=$('<h3>這是一個h3</h3>'); $('#div1').before($h3); }) </script> </head> <body> <div id="div1"> <h1> 這是一個h1元素</h1> </div> </body> </html>
已有節(jié)點的插入
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#p1').insertBefore($("#title01"));/*換兩個節(jié)點順序*/ }) </script> </head> <body> <h1 id="title01">這是一個h1元素</h1> <p id="p1">這是一個p元素</p> <span id="span01">這是一個span元素</span> </body> </html>
刪除節(jié)點
remove()
:刪除節(jié)點
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { $('#p1').insertBefore($("#title01"));/*換兩個節(jié)點順序*/ $('#p1').remove(); }) </script> </head> <body> <h1 id="title01">這是一個h1元素</h1> <p id="p1">這是一個p元素</p> <span id="span01">這是一個span元素</span> </body> </html>
關(guān)于a標(biāo)簽的問題
<a href="javascript:alert('ok?');" rel="external nofollow" >鏈接</a>
如果這樣寫就是插入JavaScript語句,彈出ok,如果是寫#就是連接到頁面頂部。
todolist網(wǎng)頁
實現(xiàn)一個用戶自己列計劃的網(wǎng)頁
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>todolist</title> <style type="text/css"> .list_con { width: 400px; margin: 50px auto 0; } .inputtxt { width: 350px; height: 30px; border: 1px solid #ccc; padding: 0px; text-indent: 10px; } .inputbtn { width: 40px; height: 32px; padding: 0px; border: 1px solid #ccc; } .list { margin: 0; padding: 0; list-style: none; margin-top: 20px; } .list li { height: 30px; line-height: 30px; border-bottom: 1px solid #ccc; } .list li span { float: left; } .list li a { float: right; text-decoration: none; margin: 0 10px; } </style> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function () { var $inputtxt = $('#txt1'); var $btn = $('#btn1'); var $ul = $('#list'); $btn.click(function () { var $val = $inputtxt.val(); /*獲取input框的值*/ if ($val == "") { alert("請輸入內(nèi)容"); return; } else { alert(1); var $li=$('<li><span>'+$val+'</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a\n' + ' href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a></li>'); $li.appendTo($ul); $inputtxt.val("");/*獲取到值之后,清空*/ var $a=$li.find('.del'); $a.click(function () { $(this).parent().remove(); }); $li.find('.up').click(function () { $(this).parent().insertBefore($(this).parent().prev()); }); $li.find('.down').click(function () { $(this).parent().insertAfter($(this).parent().next()); }); } }); $del=$(".del"); $del.click(function () { $(this).parent().remove(); }); $('.up').click(function () { $(this).parent().insertBefore($(this).parent().prev()); }); $('.down').click(function () { $(this).parent().insertAfter($(this).parent().next()); }); }) </script> </head> <body> <div class="list_con"> <h2>To do list</h2> <input type="text" name="" id="txt1" class="inputtxt"> <input type="button" name="" value="增加" id="btn1" class="inputbtn"> <ul id="list" class="list"> <li><span>學(xué)習(xí)html</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a></li> <li><span>學(xué)習(xí)css</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a></li> <li><span>學(xué)習(xí)javascript</span><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="up"> ↑ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="down"> ↓ </a><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">刪除</a></li> </ul> </div> </body> </html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun 測試上述代碼運行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery操作DOM節(jié)點方法總結(jié)》、《jQuery頁面元素操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
jquery.tableSort.js表格排序插件使用方法詳解
這篇文章主要為大家詳細介紹了jquery.tableSort.js表格排序插件使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02JQuery的read函數(shù)與js的onload不同方式實現(xiàn)
JQuery的read函數(shù)與js的onload,想必大家對這兩個方法都有所熟悉吧,接下來介紹一個實例用以上兩種方法各自實現(xiàn),感興趣的你可不要錯過了哈,希望可以幫助到你2013-03-03jQuery插件Tooltipster實現(xiàn)漂亮的工具提示
Tooltipster是一個jQuery插件用于快速創(chuàng)建HTML5校驗并且靈活的Tooltips。它可以通過CSS來改變外觀,箭頭的位置之后,鼠標(biāo),延遲/期間的外觀都可以被定義。2015-04-04jQuery-onload讓第一次頁面加載時圖片是淡入方式顯示
第一次打開一個頁面時,讓加載好的圖片先隱藏,然后再執(zhí)行動畫fadeIn,這里的load事件:當(dāng)所有子元素已經(jīng)被完全加載完成時,load事件被發(fā)送到這個元素2012-05-05