variable
搜尋節點找出文件中的heading
找出文件中的heading 1節點
x=document.getElementsByTagName("h1") for (i=0;i<x.length;i++){ console.log(x[i].outerHTML) }
找出文件中的連結(例如 ):
可以利用兩種for 迴圈的一種
方法1:
y=document.getElementsByTagName("a")
for (var i=0;i<y.length;i++){
console.log(y[i].href)
}
方法2:
y=document.getElementsByTagName("a")
//for (itemIdx=0;itemIdx<y.length;itemIdx++)
for (itemIdx in y){
console.log(y[itemIdx].href)
}
範例補充:
var x=[4,2,3,1]
for ( item in x){
console.log(x[item])
}
** other modern language**
for (item in y){
console.log(item.href)
}
搜尋節點,並過濾
找出網頁中所有鏈結中,有PDF的連結:
y=document.getElementsByTagName("a")
for (item in y){
if (y[item].href.endsWith(".pdf")){
console.log(y[item].href)
}
}