我对如何在UIWebView中执行的js中提取var的值感到困惑,我的代码是:
NSString * js = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function getTopLeftCorner() { "
"var el = document.elementFromPoint(%f, %f);"
"var pos = [el.offsetLeft, el.offsetTop];"
"var parent = el.offsetParent;"
"if (parent != el) {"
"while (parent) {"
"pos[0] += parent.offsetLeft;"
"pos[1] += parent.offsetTop;"
"parent = parent.offsetParent;"
"}"
"}"
"return pos;"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"
, pt.x, pt.y];
[self stringByEvaluatingJavaScriptFromString:js];
NSString * result = [self stringByEvaluatingJavaScriptFromString:@"getTopLeftCorner()"];
NSLog(@"RESULT IS %@", result);
知道为什么结果是空的吗?
答案 0 :(得分:0)
您的第一个语句NSString * js = [NSString stringWithFormat:@"var script
会关闭该行末尾的字符串,然后再跟踪几个C strings
。您应该从Objective-C编译器获得语法错误。
要连接字符串,请使用[NSString stringWithFormat:@"%@/%@/%@", three, two, one];
。请仔细阅读class reference以获得有关该方法的更多说明。