最近打算換個工作環(huán)境迷雪,所以就沒怎么寫東西蠢甲。面試中,被問到一些在UIWebView和WKWebView中如何與JavaScript交互的問題珊拼,索性在這里復習一下厘托,有的都忘了友雳。
CSS常用參數(shù):
是否允許用戶選擇元素的內(nèi)容,選擇值包括:
1.auto:用戶可以元素內(nèi)的內(nèi)容
2.none:用戶不能選擇任何內(nèi)容
3.text:用戶只能選擇元素內(nèi)的文本
常用語句:
1.禁用用戶選擇
[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none'"];
2.禁用長按彈出框
[self.webView stringByEvaluaingJavaScriptFromString:@"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.getElementByName('email')[0].value='hello';"]; NSLog(@"js_email_ByName==%@",js_email_ByName); NSString *js_password_byName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByName('pwd')[0].value='hello';"]; NSString *js_phone_ByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementByName('tel')[0].value='hello';"]; NSlog(@"js_phone_ByName==%@",js_phone_ByName);
6.通過id獲取與設置與上述類似
只是將@“”中的內(nèi)容換成document.getElementByIdx_x_x('phone自己替換').value='';
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 stringByEvaluatingJavaScript:@"document.getElementByIdx_x("saySome").scrollntoView("true")"];
10.改變webView尺寸時對應改變web page尺寸
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content','width=%d;',false);"(int)webView.frame.size.width]];
先羅列這么多押赊,后面再更新