Alb*_*haw 0 javascript variables objective-c ios
我有一个带有< div> 的html文件,我正在加载到UIWebView中,我需要知道div中有多少行文本,所以我可以用javascript检查:
<script>
function countLines() {
var divHeight = document.getElementById('myDiv').offsetHeight;
var lineHeight = parseInt(document.getElementById('myDiv').style.lineHeight);
var lines = divHeight / lineHeight;
alert("Lines: " + lines);
}
</script>
Run Code Online (Sandbox Code Playgroud)
它确实警告变量"行"
在Objective-C中,如何从UIWebView中检索此变量,然后在我的代码中使用它?
谢谢!
UIWebView方法stringByEvaluatingJavaScriptFromString:是JavaScript环境的唯一接口.它会计算并返回传递给它的JavaScript表达式值的字符串表示形式.所以,例如:
// simplified expression, no function needed
NSString *expr = @"document.getElementById('myDiv').offsetHeight / document.getElementById('myDiv').style.lineHeight";
// now pass it to the web view and parse the result
int lines = [[self.webView stringByEvaluatingJavaScriptFromString:expr] intValue];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5151 次 |
| 最近记录: |