[轉載] js與webview 常用交互代碼

來源:點此  http://www.cnblogs.com/iOS-mt/p/4319827.html

 

css常用參數:::

是否允許用戶選擇元素的內容,選擇值包括: 
   auto:用戶可以選擇元素內的內容 
   none:用戶不能選擇任何內容 
   text:用戶只能選擇元素內的文本 

 

常用JS語句:::

1、 //禁用用戶選擇

    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

 

2、//禁用長按彈出框

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];

 

3、//獲得UIWebView的URL地址

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];

NSLog(@"currentURL==%@",currentURL);

 

4、//獲得UIWebView的標題

NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];

NSLog(@"theTitle==%@",theTitle);

 

5、//通過name(獲得/設置)頁面元素的value值

    NSString *js_email_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].value='hello';"];

    NSLog(@"js_email_ByName==%@",js_email_ByName);

NSString *js_password_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('pwd')[0].value='hello';"];

NSLog(@"js_password_ByName==%@",js_password_ByName);

    NSString *js_phone_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('tel')[0].value='hello';"];

NSLog(@"js_phone_ByName==%@",js_phone_ByName);

 

6、//通過id(獲得/設置)頁面元素的value值

NSString *js_email_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_email').value='asdfasdf';"];

    NSLog(@"js_email_ById==%@",js_email_ById);

NSString *js_password_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_pwd').value='asdfasdf';"];

    NSLog(@"js_password_ById==%@",js_password_ById);

NSString *js_phone_ById = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x_x('_iphone_phone').value='asdfasdf';"];

     NSLog(@"js_phone_ById==%@",js_phone_ById);

 

7、//提交表單

    NSString *js_forms = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];

    NSLog(@"js_forms==%@",js_forms);

   

8、//獲得body與body之間的HTML

    NSString *allHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

NSLog(@"allHTML: %@", allHTML);

 

9、//使UIWebView的輸入框獲得焦點(但是無法,彈出iphone鍵盤)

    [webView stringByEvaluatingJavaScriptFromString:@"document.querySelector('#saySome').focus()"];

    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByIdx_x("saySome").scrollIntoView("true")"];

 

10、//改變webview尺寸時對應改變web page尺寸(web page需要有對應的處理)

    [webview stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",(int)webview.frame.size.width]];

   

11、//獲取webview顯示內容的高度

    CGFloat documentWidth = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').offsetWidth"] floatValue];

CGFloat documentHeight = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];

 

12、//通過id獲取內容

    NSString *js = @"document.getElementById('lg').innerHTML";

    NSString *pageSource = [webView stringByEvaluatingJavaScriptFromString:js];

    NSLog(@"pagesource:%@", pageSource);

 

13、//改變字體大小

  [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= ’150%’"];

 

14、//改變webView中圖片大小

    [webView stringByEvaluatingJavaScriptFromString:

     @"var script = document.createElement('script');"

     "script.type = 'text/javascript';"

     "script.text = \"function ResizeImages() { "

     "var myimg,oldwidth;"

     "var maxwidth = 300.0;" // UIWebView中顯示的圖片寬度

     "for(i=0;i <document.images.length;i++){"

     "myimg = document.images[i];"

     "if(myimg.width > maxwidth){"

     "oldwidth = myimg.width;"

     "myimg.width = maxwidth;"

     "}"

     "}"

     "}\";"

     "document.getElementsByTagName('head')[0].appendChild(script);"];

 

15、//刪除所有鏈接

     [webView stringByEvaluatingJavaScriptFromString:@"$(document).ready(function () {$(\"a\").removeAttr(\"href\");})"];

 

補充:::

UIWebview類中有有一個名爲scalesPageToFit的BOOL屬性,該屬性指定當web頁面與UIWebView的大小不一致時,是否縮放web頁面來使用 UIWebView組件的大小。默認值爲NO,即忽略web頁面與webview組件的大小關係,以頁面的原始大小進行顯示,不執行任何縮放。有時爲了保證內容出現滾動條,要確保HTML頁面的大小與webview組件的大小的一致性,同時設置webview.scrollView.scrollEnabled  = NO . 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章