jQuery DOM操作小結(jié)與實(shí)例
1. DOM Core
DOM Core并不專屬于javascript,任何一種支持DOM的程序設(shè)計(jì)語(yǔ)言都可以使用它。
它的用途并非僅限于處理網(wǎng)頁(yè),也可以用來(lái)處理任何一種使用標(biāo)記語(yǔ)言編寫(xiě)出來(lái)的文檔,如XML.
Javascript中的getElementById(),getElementByTagName(),getAttribute()和setAttribute()方法,都是dom core的組成部分。
2. HTML_DOM
使用HTML_DOM來(lái)獲取表單對(duì)象的方法
Document.forms
使用HTML_DOM來(lái)獲取某元素的src屬性的方法
Element.src
3. CSS_DOM
CSS_DOM是針對(duì)CSS的操作。在javascript中,CSS-DOM技術(shù)的主要作用是獲取和設(shè)置style對(duì)象的各個(gè)屬性。通過(guò)改變style對(duì)象的各種屬性,可以使網(wǎng)頁(yè)呈現(xiàn)出各種不同的效果。
Element.style.color = “red”;
jQuery作為javascript庫(kù),繼承并發(fā)揚(yáng)了javascript對(duì)DOM對(duì)象的操作的特性,使開(kāi)發(fā)人員能方便的操作DOM對(duì)象。
jQuery 的DOM操作方法 元素的創(chuàng)建、復(fù)制、重組、修飾。下面的例子完全可用,每一行都寫(xiě)有注釋,請(qǐng)復(fù)制代碼運(yùn)行。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title></title>
<script src="http://img.jb51.net/jslib/jquery/jquery.js" type="text/javascript"></script>
<style type="text/css">
.chapter
{
width: 42em;
}
a.link
{
text-decoration: none;
}
span.footnote
{
font-style: italic;
font-family: "Times New Roman" , Times, serif;
display: block; /*使其變成一塊一塊的*/
margin: 1em 0;
}
.text-reference
{
font-weight: bold;
}
#notes li
{
margin: 1em 0;
}
#notes
{
margin-top: 1em;
border-top: 1px solid #00ff00;
}
#footer
{
margin-top: 1em;
border-top: 1px solid #dedede; /*上邊線*/
}
.inhabitants
{
border-bottom: 1px solid #dedede;
}
.pulled-wrapper
{
background: url(pq-top.jpg) no-repeat left top;
position: absolute;
width: 160px;
right: -180px; /* 定位注釋框的橫向位置*/
padding-top: 18px;
}
.pulled
{
background: url(pq-bottom.jpg) no-repeat left bottom;
position: relative;
display: block;
width: 140px;
padding: 0 10px 24px 10px;
font: italic 1.4em "Times New Roman" , Times, serif;
}
</style>
<script type="text/javascript">
//為每個(gè)p元素添加屬性
$(document).ready(function() {
$('p').each(function(index) {
var currentClass = $(this).attr('class');
$(this).attr('class', currentClass + ' inhabitants');
});
});
//動(dòng)態(tài)為元素添加屬性
$(document).ready(function() {
$('div.chapter a[href*=cnblogs]').each(function(index) { //each好似for循環(huán),他會(huì)循環(huán)集合中所有的對(duì)象,參數(shù)一的方法是對(duì)每一個(gè)對(duì)象都執(zhí)行的操作,index是對(duì)象的索引
var $thisLink = $(this);
$(this).attr({
'rel': 'subsection ',
'id': 'blogslink-' + index,
'title': '更多' + $thisLink.text() + '的資料在馮瑞濤的博客',
'class': 'link'
});
});
});
//插入返回到上面連接
$(document).ready(function() {
$('<a id="top" name="top">新年好</a>').prependTo('body'); //初始化到body
$('div.chapter p:gt(0)').after('<a href="#top">返回到上面</a>');
//下行等價(jià)上面的哪行代碼 gt代表從第幾個(gè)元素后面的p開(kāi)始
//$('<a href="#top">返回到上面</a>').insertAfter('div.chapter p:gt(0)');
});
//
$(document).ready(function() {
$('<ol id="notes"></ol>').insertAfter('div.chapter');
$('span.footnote').each(function(index) {
$(this)
//為每一個(gè)footnote在前面動(dòng)態(tài)添加數(shù)字連接(1,2)
.before('<a href="#foot-note-' + (index + 1) + '" id="context-' + (index + 1) + '" class="context"><sup>' + (index + 1) + '</sup></a>')
//將footnote插入到ol標(biāo)簽中(不帶上面的連接,僅span),就是移動(dòng)標(biāo)簽,帶有appendTo代表將自己追加到其他元素中
.appendTo('#notes')
// 向指定元素內(nèi)容的后面追加標(biāo)簽
.append(' (<a href="#context-' + (index + 1) + '">內(nèi)容</a>)')
//將this包含在wrap的第一個(gè)參數(shù)中表示的標(biāo)記中
.wrap('<li id="foot-note-' + (index + 1) + '"></li>');
});
});
$(document).ready(function() {
$('span.pull-quote').each(function(index) {
//獲得父元素p
var $parentParagraph = $(this).parent('p');
//設(shè)置p標(biāo)簽為相對(duì)定位,否則無(wú)法對(duì)其位置進(jìn)行操作
$parentParagraph.css('position', 'relative');
//復(fù)制一份拷貝,span.pull-quote clone(false);代表僅復(fù)制標(biāo)記本身不復(fù)制其內(nèi)容
var $clonedCopy = $(this).clone();
$clonedCopy
.addClass('pulled') //添加樣式,擁有下面的背景
.find('span.drop') //找到其中的span.drop,此時(shí)對(duì)象已經(jīng)是span.drop了
.html('…') //為span.drop 設(shè)置html文檔
.end() //返回沒(méi)有被改變前的那個(gè)jQuery對(duì)象狀態(tài)
.prependTo($parentParagraph) //將這個(gè)span追加到指定的元素中去
.wrap('<div class="pulled-wrapper"></div>'); //再其本身包含在div內(nèi)容中<div><span>
var clonedText = $clonedCopy.text(); //獲得文本,去掉了html
$clonedCopy.html(clonedText); //將文本以Html的形式插入到內(nèi)容中,相當(dāng)于替換html內(nèi)容
});
});
</script>
</head>
<body>
<form id="form1">
<span class="footnote">佳月</span> <span class="footnote">Terry.Feng.C</span> <span
class="footnote">馮瑞濤</span>
<div class="chapter">
<p>
1. <a >jQuery</a>動(dòng)態(tài)為鏈接添加屬性。</p>
<p>
2. <a >CSLA.Net</a>業(yè)務(wù)層最強(qiáng)框架。<span class="pull-quote">CSLA注釋<span class="drop">省略部分</span></span></p>
<p>
3. <a >DNN</a>免費(fèi)開(kāi)源的CMS系統(tǒng)。<span class="pull-quote">DNN注釋<span class="drop">省略部分</span></span></p>
</div>
<div id="footer">
馮瑞濤的博客</div>
</form>
</body>
</html>
- jQuery移動(dòng)和復(fù)制dom節(jié)點(diǎn)實(shí)用DOM操作案例
- jQuery的DOM操作之刪除節(jié)點(diǎn)示例
- jQuery學(xué)習(xí)筆記之jQuery的DOM操作
- jQuery DOM操作 基于命令改變頁(yè)面
- jQuery 選擇器、DOM操作、事件、動(dòng)畫(huà)
- jQuery常見(jiàn)面試題之DOM操作詳析
- 淺談jQuery 選擇器和dom操作
- Jquery基礎(chǔ)教程之DOM操作
- 鋒利的jQuery jQuery中的DOM操作
- jquery中dom操作和事件的實(shí)例學(xué)習(xí) 仿yahoo郵箱登錄框的提示效果
- jQuery DOM操作實(shí)例
- jQuery中DOM常見(jiàn)操作實(shí)例小結(jié)
相關(guān)文章
jquery.jstree 增加節(jié)點(diǎn)的雙擊事件代碼
本文基于 jsTree 1.0-rc1 版本增加節(jié)點(diǎn)的雙擊事件。2010-07-07使用jquery動(dòng)態(tài)加載Js文件和Css文件
這篇文章主要為大家詳細(xì)介紹了使用jquery動(dòng)態(tài)加載Js文件和Css文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-10-103種不同的ContextMenu右鍵菜單實(shí)現(xiàn)代碼
本文給大家分享3種不同的ContextMenu右鍵菜單實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11jQuery EasyUI API 中文文檔 - ComboGrid 組合表格
jQuery EasyUI API 中文文檔 - ComboGrid 組合表格,需要的朋友可以參考下。2011-10-10jQuery自定義動(dòng)畫(huà)函數(shù)實(shí)例詳解(附demo源碼)
這篇文章主要介紹了jQuery自定義動(dòng)畫(huà)函數(shù)實(shí)現(xiàn)方法,形式實(shí)例分析了jQuery通過(guò)插件結(jié)合數(shù)學(xué)運(yùn)算實(shí)現(xiàn)滑塊動(dòng)畫(huà)運(yùn)動(dòng)的效果,并附完整demo源碼供讀者下載,需要的朋友可以參考下2015-12-12jquery通過(guò)closest選擇器修改上級(jí)元素的方法
這篇文章主要介紹了jquery通過(guò)closest選擇器修改上級(jí)元素的方法,實(shí)例分析了jQuery中closest選擇器的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03利用jquery.qrcode在頁(yè)面上生成二維碼且支持中文
這篇文章主要介紹了利用jquery.qrcode在頁(yè)面上生成二維碼且支持中文。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02關(guān)于jQuery對(duì)象數(shù)據(jù)緩存Cache原理以及jQuery.data詳解
網(wǎng)上有很多教你怎么使用jQuery.data(..)來(lái)實(shí)現(xiàn)數(shù)據(jù)緩存,但有兩個(gè)用戶經(jīng)常使用的data([key],[value])和jQuery.data(element,[key],[value])幾乎沒(méi)有什么文章說(shuō)清楚它們兩的區(qū)別,所以我用到了,研究下分享給大家。2013-04-04jQuery如何獲取同一個(gè)類標(biāo)簽的所有值(默認(rèn)無(wú)法獲取)
jQuery總是只返回第一個(gè)類標(biāo)簽的值,所以無(wú)法達(dá)到我們的要求,那么jQuery如何獲取同一個(gè)類標(biāo)簽的所有的值,下面與大家分享方法2014-09-09