js獲取html頁面節(jié)點方法(遞歸方式)
很久沒有操作過遞歸調(diào)用了??赐曛?,驀然驚醒??!
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>統(tǒng)計Element節(jié)點</title>
<script language="javascript">
var elementName="";
function countTotalElement(node)
{
///Attribute nodeType值為2,表示節(jié)點屬性
///Comment nodeType值為8,表示注釋文本
///Document nodeType值為9,表示Document
///DocumentFragment nodeType值為11,表示Document片段
///Element nodeType值為1,表示元素節(jié)點
///Text nodeType值為3,表示文本節(jié)點
var total=0;
if(node.nodeType==1) //1代表節(jié)點的類型為Element
{
total++;
elementName=elementName+node.tagName+"\r\n";
}
var childrens=node.childNodes;
for(var i=0;i<childrens.length;i++)
{
total+=countTotalElement(childrens[i]);
}
return total;
}
</script>
</head>
<body>
<h1>測試</h1>
<table width="100" border="2" cellpadding="0" cellspacing="0">
<tr><td>
<form name="form1" action="" method="post">
<input type="text" name="ipput1" value="測試"><br />
<input type="password" name="password" value="">
</form>
</td></tr>
</table>
<a href="javascript:void(0)" onClick="alert('標記總數(shù)'+countTotalElement(document)+'\r\n 全部標記如下:\r\n'+elementName);">開始測試</a>
</body>
</html>
其實,通過遞歸調(diào)用也可以實現(xiàn) 想百度蜘蛛爬蟲一樣的效果!這個值得一試,或許可以通過這個方法,寫一個sitemap生成器!
相關文章
JavaScript實現(xiàn)橫線提示輸入驗證碼隨輸入驗證碼輸入消失的方法
這篇文章主要介紹了JavaScript實現(xiàn)橫線提示輸入驗證碼隨輸入驗證碼輸入消失的方法,非常實用,在微信開發(fā)中經(jīng)常會用到,需要的朋友可以參考下2016-09-09
JavaScript中Object.prototype.toString方法的原理
這篇文章主要介紹了JavaScript中Object.prototype.toString方法的原理的相關資料,需要的朋友可以參考下2016-02-02
微信小程序使用toast消息對話框提示用戶忘記輸入用戶名或密碼功能【附源碼下載】
這篇文章主要介紹了微信小程序使用toast消息對話框提示用戶忘記輸入用戶名或密碼功能,結(jié)合實例形式詳細分析了toast組件實現(xiàn)消息提示功能的相關操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12

