標籤:
擷取所有html:NSString *lJs = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
擷取body:
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"];
去除某個屬性:
[webView stringByEvaluatingJavaScriptFromString:[NSStringstringWithFormat:@"document.images[%d].removeAttribute("width")", i]];
修改圖片的地址
[webView stringByEvaluatingJavaScriptFromString:@"document.images[0].src="http://www.baidu.com/W020110907540066716627.jpg";document.images[1].src="http://www.baidu.com/W020120807265226549111.jpg""];
[webView stringByEvaluatingJavaScriptFromString:[NSStringstringWithFormat:@"document.images[%d].src="%@"",i, imageName]];
教程:
http://www.w3school.com.cn/htmldom/met_doc_getelementsbytagname.asp
Document對象內容集合
document 文擋對象 -JavaScript指令碼語言描述
———————————————————————
注:頁面上元素name屬性和JavaScript引用的名稱必須一致包括大小寫
否則會提示你一個錯誤資訊 “引用的元素為空白或者不是對象\\\\\”
———————————————————————
對象屬性
document.title //設定文檔標題等價於HTML的title標籤
document.bgColor //設定頁面背景色
document.fgColor //設定前景色彩(文本顏色)
document.linkColor //未點擊過的連結顏色
document.alinkColor //啟用連結(焦點在此連結上)的顏色
document.vlinkColor //已點擊過的連結顏色
document.URL //設定URL屬性從而在同一視窗開啟另一網頁
document.fileCreatedDate //檔案建立日期,唯讀屬性
document.fileModifiedDate //檔案修改日期,唯讀屬性
document.fileSize //檔案大小,唯讀屬性
document.cookie //設定和讀出cookie
document.charset //設定字元集 簡體中文:gb2312
———————————————————————
常用對象方法
document.write() //動態向頁面寫入內容
document_createElement_x_x_x_x_x_x(Tag) //建立一個html標籤對象
document.getElementByIdx_x_x_x_x_x_x(ID) //獲得指定ID值的對象
document.getElementsByName(Name) //獲得指定Name值的對象
document.body.a(oTag)
———————————————————————
body-主體子物件
document.body //指定文檔主體的開始和結束等價於body>/body>
document.body.bgColor //設定或擷取對象後面的背景顏色
document.body.link //未點擊過的連結顏色
document.body.alink //啟用連結(焦點在此連結上)的顏色
document.body.vlink //已點擊過的連結顏色
document.body.text //文本色
document.body.innerText //設定body>…/body>之間的文本
document.body.innerHTML //設定body>…/body>之間的HTML代碼
document.body.topMargin //頁面上邊距
document.body.leftMargin //頁面左邊距
document.body.rightMargin //頁面右邊距
document.body.bottomMargin //頁面下邊距
document.body.background //背景圖片
document.body.a(oTag)//動態產生一個HTML對象
常用對象事件
document.body.onclick=”func()”//滑鼠指標單擊對象是觸發
document.body.onmouseover=”func()” //滑鼠指標移到對象時觸發
document.body.onmouseout=”func()” //滑鼠指標移出對象時觸發
———————————————————————
location-位置子物件
document.location.hash// #號後的部分
document.location.host // 網域名稱+連接埠號碼
document.location.hostname // 網域名稱
document.location.href // 完整URL
document.location.pathname // 目錄部分
document.location.port // 連接埠號碼
document.location.protocol // 網路通訊協定(http:)
document.location.search // ?號後的部分
documeny.location.reload()//重新整理網頁
document.location.reload(URL) //開啟新的網頁
document.location.assign(URL) //開啟新的網頁
document.location.replace(URL) //開啟新的網頁
———————————————————————
selection-選區子物件
document.selection
———————————————————————
images集合(頁面中的圖象)
a)通過集合引用
document.images //對應頁面上的img標籤
document.images.length //對應頁面上img標籤的個數
document.images[0] //第1個img標籤
document.images[i] //第i-1個img標籤
b)通過nane屬性直接引用
img name=”oImage”
document.images.oImage //document.images.name屬性
c)引用圖片的src屬性
document.images.oImage.src //document.images.name屬性.src
d)建立一個圖象
var oImage
oImage = new Image()
document.images.oImage.src=”1.jpg”
同時在頁面上建立一個img /標籤與之對應就可以顯示
———————————————————————-
forms集合(頁面中的表單)
a)通過集合引用
document.forms //對應頁面上的form標籤
document.forms.length //對應頁面上/formform標籤的個數
document.forms[0] //第1個/formform標籤
document.forms[i] //第i-1個/formform標籤
document.forms[i].length //第i-1個/formform中的控制項數
document.forms[i].elements[j] //第i-1個/formform中第j-1個控制項
b)通過標籤name屬性直接引用
/formform name=”Myform”>inputname=”myctrl”/>/form
document.Myform.myctrl //document.表單名.控制項名
c)訪問表單的屬性
document.forms[i].name //對應form name>屬性
document.forms[i].action //對應/formform action>屬性
document.forms[i].encoding //對應/formform enctype>屬性
document.forms[i].target //對應/formform target>屬性
document.forms[i].a(oTag)//動態插入一個控制項
document.all.oDiv //引用圖層oDiv
document.all.oDiv.style.display=” //圖層設定為可視
document.all.oDiv.style.display=”none” //圖層設定為隱藏
document.getElementId(”oDiv”)//通過getElementId引用對象
document.getElementId(”oDiv”).style=”
document.getElementId(”oDiv”).display=”none”
圖層對象的4個屬性
document.getElementByIdx_x_x_x_x_x_x(”ID”).innerText //動態輸出文本
document.getElementByIdx_x_x_x_x_x_x(”ID”).innerHTML //動態輸出HTML
document.getElementByIdx_x_x_x_x_x_x(”ID”).outerText //同innerText
document.getElementByIdx_x_x_x_x_x_x(”ID”).outerHTML //同innerHTML
document.getElementsByName 和document.getElementsByTagName
當頁面上的控制項同名且多個的時候,從程式的嚴密角度出發,需要判斷長度,而且有長度和沒長度是兩種引用方法.
oEle= document.all.aaa ;//這裡有一個aaa的對象,但我們不知道它現在長度是多少,所以沒辦法對它操作.因此,我們要先做判斷長度的過程.如下:
if(oEle.length){}else{};
在兩種情況下,花括弧裡面的內容寫法也是不一樣的:
if(oEle.length){
for(var i = 0 ;i<oEle.length;i++){
oEle[i].value..
}
}
else{
oEle.value..
};
但是這樣寫是不是太複雜了點?而且當花括弧裡面的代碼比較多的時候,我們要寫兩遍代碼,暈了先~
還好有
document.getElementsByName()
這個方法.它對一個和多個的處理是一樣的,我們可以用:
oEle = document.getElementsByName(‘aaa‘)
來引用
當oEle只有1個的時候,那麼就是oEle[0],有多個的時候,用下標法oEle[i]迴圈擷取,是不是很簡單?
值得一提的是它對Name和ID的同樣有效. (它只能應用到document對象)
<div id=radiodiv language=javascript >
<INPUT name=radio1 value=1type="radio">
<INPUT name=radio1 value=0type="radio" CHECKED>
</div>
document.getElementsByName("radio1").item(0).value結果是 1
但是.相對應的,還有另一個方法,可以應用的對象會更廣一點:
getElementsByTagName
<div id=radiodiv language=javascript >
<INPUT name=radio1 value=1type="radio" id=myRadio1>
<INPUT name=radio1 value=0type="radio" id=myRadio2 CHECKED >
</div>
radiodiv.getElementsByTagName_r("input").item(0).value結果是 1
,比如我知道了一個<DIV ID=‘aaa‘><input name=input1value=1 type=radio ><input name=input1 value=2type=radio>......</DIV>我要取DIV裡面的所有input,這樣寫就可以了:
aaa.getElementsByTagName_r(‘INPUT‘)
這樣就有效可以和別的DIV(比如說有個叫bbb的DIV,裡面的也是一樣的input)相區別.
同getElementsByTagName相對應,
還有一個document.body.all.tags()
能用這個方法的對象比getElementsByTagName要小得多.但比getElementsByName要多.
到這裡我們還要提一下getElementById
它也是只有document對象才能使用,而且返回的是數組的第一個元素,呵呵,它的方法名都寫明了是getElement而不是getElements
<div id=radiodiv language=javascript >
<INPUT name=radio1 value=1type="radio" id=myRadio1 >
<INPUT name=radio1 value=0type="radio" id=myRadio2 value="myVal2" CHECKED >
</div>
document.getElementByIdx_x_x_x_x("myRadio2").value結果是 myVal2
iOS中JS的操作