最近打算換個(gè)工作環(huán)境,所以就沒(méi)怎么寫東西。面試中,被問(wèn)到一些在UIWebView和WKWebView中如何與JavaScript交互的問(wèn)題,索性在這里復(fù)習(xí)一下,有的都忘了。
CSS常用參數(shù):
是否允許用戶選擇元素的內(nèi)容,選擇值包括:
1.auto:用戶可以元素內(nèi)的內(nèi)容
2.none:用戶不能選擇任何內(nèi)容
3.text:用戶只能選擇元素內(nèi)的文本
常用語(yǔ)句:
1.禁用用戶選擇
[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none'"];
2.禁用長(zhǎng)按彈出框
[self.webView stringByEvaluaingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none'"];
3.獲得UIWebView的URL地址
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"]; NSLog(@"currentURL == %@",currentURL);
4.獲得UIWebView的標(biāo)題
NSString *theTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; NSLog(@"theTitle ==%@",theTitle);
5.通過(guò)name(獲得或設(shè)置)界面元素的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.通過(guò)id獲取與設(shè)置與上述類似
只是將@“”中的內(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的輸入框獲得焦點(diǎn),但是無(wú)法彈出iPhone鍵盤
[webView stringByEvaluatingJavaScriptFromString:@"document.querySelector('#saySome').focus()"]; [webView stringByEvaluatingJavaScript:@"document.getElementByIdx_x("saySome").scrollntoView("true")"];
10.改變webView尺寸時(shí)對(duì)應(yīng)改變web page尺寸
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content','width=%d;',false);"(int)webView.frame.size.width]];
先羅列這么多,后面再更新