gpt4 book ai didi

javascript - 检查 UIWebView 中是否存在类

转载 作者:行者123 更新时间:2023-11-30 09:06:42 26 4
gpt4 key购买 nike

我想使用 javascript 检查 UIWebView 中是否存在类。这是我目前所拥有的:

NSString* checkForWaldoCmd = [[NSString alloc] initWithString:@"document.getElementsByClassName('waldo');"];
NSString* wheresWaldo = [myUIWebView stringByEvaluatingJavaScriptFromString:checkForWaldoCmd];

我基本上想以某种方式检查页面中是否存在“waldo”类。当我运行上面的代码时,无论该类是否存在,我都会得到一个空字符串。有什么建议吗?

最佳答案

使用 stringByEvaluatingJavaScriptFromString: 的主要技巧是计算表达式,该表达式的计算结果可以很容易地转换为字符串( native 简单类型通常有效:float、int 和 string)。在您的示例中,@"document.getElementsByClassName('waldo');"将是没有简单字符串表示的 NodeList 类型,这就是为什么你得到一个空字符串。例如,尝试获取类 waldo 的元素列表的长度:

NSString *count = [myUIWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('waldo').length;"];
if ([count intValue] > 0){
NSLog(@"Have elements of class waldo");
}else{
NSLog(@"Don't have elements of class waldo");
}

关于javascript - 检查 UIWebView 中是否存在类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4581083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com