webView与js交互
来源:互联网 发布:床垫品牌 知乎 编辑:程序博客网 时间:2024/06/07 06:38
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与js交互
- webview与js交互
- webview与js交互
- webview与js交互
- webview与js交互
- webview与js交互
- WebView与JS交互
- webView与js交互
- webView与js交互
- webview与js交互
- webview与js交互
- webview与js交互
- webview与js交互
- WebView与JS交互
- webview与js交互
- Webview 与JS 交互
- webview与js交互
- WebView与Js交互
- 深入分析C++中char * 和char []的区别
- TCP选项之SO_LINGER
- CSS 的overflow:hidden 属性详细解释
- Android开发中一些疑难异常解决
- MCBrowserViewController
- webView与js交互
- Usb Audio Device Descriptor(2) Class-Specific Audio Control Interface Header Descriptor
- Apache优化:修改最大并发连接数
- Java基础之volatile,static,synchronized的区别
- 为data磁盘组删除其中一个盘(asm external data盘组中有两块盘)
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- IAR error: a declaration cannot have a label
- javaEE之------SpringMVC中ResourceBundleViewResolver简单应用
- php读取超大日志文件