JQuery常見節(jié)點(diǎn)操作實(shí)例分析
本文實(shí)例講述了JQuery常見節(jié)點(diǎn)操作。分享給大家供大家參考,具體如下:
插入節(jié)點(diǎn)
append()和appengTo():在現(xiàn)存元素內(nèi)部,從后面插入
prepend()和prependTo():在現(xiàn)存元素外部,從前面插入
after()和insertAfter():在現(xiàn)存元素外部,從后面插入
before()和insertBefore():在現(xiàn)存元素外部,從前面插入
新建節(jié)點(diǎn)的插入
<!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('')這種用字符串的方式塞進(jìn)去的性能是最高的,
// 但是有點(diǎn)時(shí)候不方便用,因?yàn)檫@樣會(huì)重寫div1里面的元素
$a=$('<a href="#" rel="external nofollow" >鏈接</a>>');
$('#div1').append($a);/*在最后加入字符串,append從現(xiàn)成的元素的后面插入元素*/
$a.appendTo($('#div1'));/*和append效果相同*/
$p=$('<p>這是一個(gè)p標(biāo)簽</p>');
$("#div1").prepend($p);
$h2=$('<h2>這是一個(gè)h2</h2>');
$('#div1').after($h2);
$h3=$('<h3>這是一個(gè)h3</h3>');
$('#div1').before($h3);
})
</script>
</head>
<body>
<div id="div1">
<h1> 這是一個(gè)h1元素</h1>
</div>
</body>
</html>
已有節(jié)點(diǎn)的插入
<!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"));/*換兩個(gè)節(jié)點(diǎn)順序*/
})
</script>
</head>
<body>
<h1 id="title01">這是一個(gè)h1元素</h1>
<p id="p1">這是一個(gè)p元素</p>
<span id="span01">這是一個(gè)span元素</span>
</body>
</html>
刪除節(jié)點(diǎn)
remove():刪除節(jié)點(diǎn)
<!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"));/*換兩個(gè)節(jié)點(diǎn)順序*/
$('#p1').remove();
})
</script>
</head>
<body>
<h1 id="title01">這是一個(gè)h1元素</h1>
<p id="p1">這是一個(gè)p元素</p>
<span id="span01">這是一個(gè)span元素</span>
</body>
</html>
關(guān)于a標(biāo)簽的問題
<a href="javascript:alert('ok?');" rel="external nofollow" >鏈接</a>
如果這樣寫就是插入JavaScript語句,彈出ok,如果是寫#就是連接到頁面頂部。
todolist網(wǎng)頁
實(shí)現(xiàn)一個(gè)用戶自己列計(jì)劃的網(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("請(qǐng)輸入內(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代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun 測試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery操作DOM節(jié)點(diǎn)方法總結(jié)》、《jQuery頁面元素操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery中each()、find()和filter()等節(jié)點(diǎn)操作方法詳解(推薦)
- jQuery DOM插入節(jié)點(diǎn)操作指南
- jQuery遍歷DOM節(jié)點(diǎn)操作之filter()方法詳解
- JQuery 選擇器、DOM節(jié)點(diǎn)操作練習(xí)實(shí)例
- jQuery DOM刪除節(jié)點(diǎn)操作指南
- 淺談事件冒泡、事件委托、jQuery元素節(jié)點(diǎn)操作、滾輪事件與函數(shù)節(jié)流
- jQuery中 DOM節(jié)點(diǎn)操作方法大全
- 有關(guān)jquery與DOM節(jié)點(diǎn)操作方法和屬性記錄
- jquery對(duì)dom節(jié)點(diǎn)的操作【推薦】
相關(guān)文章
jquery.tableSort.js表格排序插件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了jquery.tableSort.js表格排序插件使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
jQuery中innerHeight()方法用法實(shí)例
這篇文章主要介紹了jQuery中innerHeight()方法用法,實(shí)例分析了innerHeight()方法的功能、定義及獲取第一個(gè)匹配元素內(nèi)部區(qū)域高度的使用技巧,需要的朋友可以參考下2015-01-01
JQuery的read函數(shù)與js的onload不同方式實(shí)現(xiàn)
JQuery的read函數(shù)與js的onload,想必大家對(duì)這兩個(gè)方法都有所熟悉吧,接下來介紹一個(gè)實(shí)例用以上兩種方法各自實(shí)現(xiàn),感興趣的你可不要錯(cuò)過了哈,希望可以幫助到你2013-03-03
基于jQuery中對(duì)數(shù)組進(jìn)行操作的方法
本篇文章小編將為大家介紹,基于jQuery中對(duì)數(shù)組進(jìn)行操作的方法。有需要的朋友可以參考一下2013-04-04
jQuery插件Tooltipster實(shí)現(xiàn)漂亮的工具提示
Tooltipster是一個(gè)jQuery插件用于快速創(chuàng)建HTML5校驗(yàn)并且靈活的Tooltips。它可以通過CSS來改變外觀,箭頭的位置之后,鼠標(biāo),延遲/期間的外觀都可以被定義。2015-04-04
jQuery-onload讓第一次頁面加載時(shí)圖片是淡入方式顯示
第一次打開一個(gè)頁面時(shí),讓加載好的圖片先隱藏,然后再執(zhí)行動(dòng)畫fadeIn,這里的load事件:當(dāng)所有子元素已經(jīng)被完全加載完成時(shí),load事件被發(fā)送到這個(gè)元素2012-05-05

