JavaScript 高級(jí)篇之DOM文檔,簡(jiǎn)單封裝及調(diào)用、動(dòng)態(tài)添加、刪除樣式(六)
1、DOM的架構(gòu)
<html>
<head>
<title>document</title>
</head>
<body>
<h1>CSS Demo</h1>
<p>我喜歡美女,特別是高個(gè)的美女</p>
</body>
</html>
這個(gè)文檔的DOM表示如下圖:
圖片表示一個(gè)HTML文檔的樹(shù).
所有DOM樹(shù)結(jié)構(gòu)表現(xiàn)為不同種類(lèi)的Node對(duì)象的一個(gè)數(shù),firstChild,lastChild,nextSibling,previousSibling和ParentNode屬性提供遍歷節(jié)點(diǎn)的樹(shù)的一種辦法,appendChild,removeChild,replaceChildh和insertBefore這樣的方法可以像文檔中添加節(jié)點(diǎn)或者從文檔中刪除節(jié)點(diǎn)。不明白沒(méi)關(guān)系接下來(lái)我將用大量的例子讓你明白。
1、先創(chuàng)建一個(gè)使用CSS美化的列表<style type="text/css">
body{ margin:0px; padding:0px; }
#container{font-family:tahoma;font-size:14px;border:solid 1px #99ffcc; width:200px;height:140px; float:left; }
#container ul{list-style:none;padding:1px 0px 0px 0px; margin:0px;}
#container ul li{ border-bottom:solid 1px #99ffcc; margin:0px;height:27px;}
#container ul li a{background-color:gray;text-decoration:none;display:block; border-left:solid 10px red;margin:0px; padding:5px 0px 5px 10px;}
#container ul li a:hover{background-color:red; color:#000000; }
</style>
2、加一個(gè)div 元素.
<div id="container">
<ul id="list">
<li><a href="#">Home</a></li>
<li id="myblog"><a href="#">MyBlog</a></li>
<li><a href="#">Sport</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Contane</a></li>
</ul>
</div>
3、你現(xiàn)在應(yīng)該看到如下圖:
var Tools = {};
Tools.getElementCount = function(e){
var count =0;
elementTotal(e);
document.table.txt.value = "element:"+ count;
function elementTotal(e)
{
if(e.nodeType == 1) count++;
var children = e.childNodes;
for(var i = 0;i<children.length;i++)
{
elementTotal(children[i]);
}
}
};
備注:大家使用可以再body加入<button type ="button" onclick = "alert(Tools.getElementCount(document))">獲取元素個(gè)數(shù)</button>
5、將文本全部大寫(xiě)
Tools.ModifyElement = function modify(e){
if(e.nodeType == 3)
e.data = e.data.toUpperCase();
else
{
for(var i = e.firstChild;i!=null;i=i.nextSibling)
modify(i);
}
};
備注:大家使用可以再body加入<button type ="button" onclick = "Tools.ModifyElement(document)">大寫(xiě)</button>
效果:
Tools.documentSort = function(e){
var textArray = [];
if(typeof e =="string") e = document.getElementById(e);
for(var x = e.firstChild; x!= null;x=x.nextSibling)
if(x.nodeType == 1) textArray.push(x);
textArray.sort(function(n,m){
var s = n.firstChild.firstChild.data;
var t = m.firstChild.firstChild.data;
if(s>t) return -1;
else if(s<t) return 1;
else return 0;
});
備注:大家使用可以再body加入<button type ="button" onclick = "Tools.documentSort('list')">排序</button>
效果:
Tools.insertElement = function(n,e){
if(typeof n == "string") n = document.getElementById(n);
var li = document.createElement(e);
var a = document.createElement("a");
a.setAttribute("href","#");
var txt = document.createTextNode("HotBlog");
a.appendChild(txt);
li.appendChild(a);
var parent = n.parentNode;
parent.insertBefore(li,n);
};
備注:大家使用可以再body加入<button type ="button" onclick="Tools.insertElement('myblog','li');">插入</button>
效果:
1、樣式表
.tooltip{background:url('2.jpg'); border:solid 1px #99ffcc; width:200px;height:200px;}//這里的圖片大家要該一下
.toolcontent{background-color:#ffffff; border:solid 1px #99ff00; padding:5px; font:tahoma 12px; color:#000000;}
2、javascript類(lèi)
function Tooltip()
{
this.tooltip = document.createElement("div");
this.tooltip.style.position = "absolute";
this.tooltip.className = "tooltip";
this.content = document.createElement("div");
this.content.style.position = "relative";
this.content.className = "toolcontent";
this.tooltip.appendChild(this.content);
}
Tooltip.prototype.show = function(text,x,y)
{
this.content.innerHTML = text;
this.tooltip.style.left = x+"px";
this.tooltip.style.top = y+"px";
this.tooltip.style.visibility = "visible";
if(this.tooltip.parentNode != document.body)
document.body.appendChild(this.tooltip);
};
Tooltip.prototype.hide = function(){ this.tooltip.style.visibility ="hidden";};
var t = new Tooltip();
function hide()
{
t.hide();
}
function show()
{
t.show("hello ",300,0);
}
function init()
{
document.operator.show.onclick = show;
document.operator.hide.onclick = hide;
}
備注:配合上面使用必須還完成以下步驟:1、將body中的onload=init();2 在body中添加 :
<form name = "operator">
<input type = "button" value = "隱藏" name = "hide"/>
<input type = "button" value = "顯示" name = "show">
</form>
效果:(隱藏看到什么了)
1、樣式表
.container{font-family:tahoma;font-size:14px;border:solid 1px #99ffcc; width:200px;height:140px;float:left;}
.container ul{list-style:none;padding:1px 0px 0px 0px; margin:0px;}
.container ul li{ border-bottom:solid 1px #99ffcc; margin:0px;height:27px;}
.container ul li a{background-color:gray;text-decoration:none;display:block; border-left:solid 10px red;margin:0px; padding:5px 0px 5px 10px;}
.container ul li a:hover{background-color:red; color:#ffffff; }
2、工具函數(shù)(動(dòng)態(tài)添加、刪除樣式)
var CSSclass = {};
CSSclass.is = function(e,c){
if(typeof e == "string") e = document.getElementById(e);
var classes = e.className;
if(!classes) return false;
if(classes == c) return true;
return e.className.search("\\b" +c +"\\b*") != -1;
};
CSSclass.add = function(e,c){
if(typeof e == "string") e = document.getElementById(e);
if(CSSclass.is(e,c))return;
//if(e.className) c=""+c;
e.className += c;
};
CSSclass.remove = function(e,c){
if(typeof e == "string") e = document.getElementById(e);
//e.id = e.id.replace(new RegExp("\\b" +e.id +"\\b\\s*","g"),"");
e.className = e.className.replace(new RegExp("\\b"+c+"\\b\\s*","g"),"");
};
3、在body中加入如下元素
<div id="con">
<ul id="list">
<li><a href="#">Home</a></li>
<li id="myblog"><a href="#">MyBlog</a></li>
<li><a href="#">Sport</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Content</a></li>
</ul>
<button type="button" name ="add" onclick = "CSSclass.add('con','container');">動(dòng)態(tài)添加樣式</button>
<button type="button" name ="remove" onclick ="CSSclass.remove('con','container');">動(dòng)態(tài)刪除樣式</button>
效果:
沒(méi)添加樣式的樣子
加了樣式之后。
(很多沒(méi)有備注,大家有問(wèn)題可以給我留言!)
相關(guān)文章
總結(jié)JavaScript在IE9之前版本中內(nèi)存泄露問(wèn)題
本篇文章給大家總結(jié)了JavaScript在IE9之前版本中內(nèi)存泄露問(wèn)題,對(duì)此有興趣的朋友可以學(xué)習(xí)下。2018-04-04javascript中call apply 與 bind方法詳解
網(wǎng)上文章雖多,大多復(fù)制粘貼,且晦澀難懂,我希望能夠通過(guò)這篇文章,能夠清晰的提升對(duì)apply、call、bind的認(rèn)識(shí),并通過(guò)一些具體的示例給大家展示下這3個(gè)方法的用法,希望大家能夠喜歡。2016-03-03Three.js源碼閱讀筆記(基礎(chǔ)的核心Core對(duì)象)
Three.js是一個(gè)比較偉大的webgl開(kāi)源庫(kù),它簡(jiǎn)化了瀏覽器3D編程,使得使用JavaScript在瀏覽器中創(chuàng)建復(fù)雜的場(chǎng)景變得容易很多接下來(lái)先從最基礎(chǔ)的核心(Core)對(duì)象開(kāi)始,感興趣的朋友可以參考下2012-12-12JavaScript While 循環(huán)基礎(chǔ)教程
只要指定條件為 true,循環(huán)就可以一直執(zhí)行代碼,2007-04-04a標(biāo)簽的href和onclick 的事件的區(qū)別介紹
a標(biāo)簽的href與onclick事件,想必大家不陌生吧,至于它們有什么區(qū)別,你知道嗎?下面就為大家介紹下,感興趣的朋友可以學(xué)習(xí)下,希望對(duì)大家有所幫助2013-07-07JavaScript DOM 學(xué)習(xí)第三章 內(nèi)容表格
在這一章我會(huì)解釋我的網(wǎng)站上的所有頁(yè)面都在運(yùn)行的內(nèi)容表格的代碼。他會(huì)生產(chǎn)這個(gè)頁(yè)面的所有h3和h4的列表,然后給他們添加一個(gè)鏈接。2010-02-02