我正在尝试实现一个相对简单的结果 - 在加载页面时关注文本框。我使用这段代码:
[_webView setKeyboardDisplayRequiresUserAction:NO];
NSString *javaString = [NSString stringWithFormat:@"document.getElementById('keyboard').focus()"];
NSString *result = [_webView stringByEvaluatingJavaScriptFromString:javaString];
它通过在我的应用程序中单击按钮来调用。此代码在测试页面上有效,但在实际站点上无效。最糟糕的是,我不知道如何调试它。
没有任何反应,结果始终为空字符串。如何检查JS代码是否有错误?还是找不到对象?或者它不能专注于它?
更新
正如@subzero 正确指出的那样,我忘记了问题中实际的 focus() 方法。现在是正确的。
最佳答案
你忘了添加 focus
Action
document.getElementById('keyboard').focus();"
will do the trick
About checking JS errors. There are 2 ways to do it.
The easy one: Add this (I'm ussing jQuery). It will show the errors at the bottom of the page
$( document ).ready(function() {window.onerror = function(err) { log('window.onerror: ' + err) } var uniqueId = 1 function log (message, data) { var log = document.getElementById('log') var el = document.createElement('div') el.className = 'logLine' el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data) if (log.children.length) { log.insertBefore(el, log.children[0]) } else { log.appendChild(el) } }
在您的 html 中:
<div id='log'></div>
关于javascript - UIWebView stringByEvaluatingJavaScriptFromString 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051804/