我的网页上有一个 Javascript 函数,正在 UIWebView 中显示:
$(document).ready(function() {
// index to reference the next/prev display
var i = 0;
// get the total number of display sections
// where the ID starts with "display_"
var len = $('div[id^="hl"]').length;
// for next, increment the index, or reset to 0, and concatenate
// it to "display_" and set it as the hash
$('#next').click(function() {
++i;
window.location.hash = "hl" + i;
return false;
});
// for prev, if the index is 0, set it to the total length, then decrement
// it, concatenate it to "display_" and set it as the hash
$('#prev').click(function() {
if (i > 1)
--i;
window.location.hash = "hl" + i;
return false;
});
});
所以我需要做的是在单击我的 UIButton 时模拟 anchor 单击:
- (IBAction)next:(id)sender {
[animalDesciption stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"next\").click();"];
}
但这行不通! 它在 HTML 页面上效果很好,只需单击 id 为“next”的 anchor 即可。
有什么想法为什么点击按钮时不起作用?
顺便说一句,我可以使用我当前的设置调用标准 javascript 函数,例如 myFunc()
,但它不会执行类似的操作!
如有任何想法,我们将不胜感激!
最佳答案
您可以为下一个和上一个实现 JavaScript 函数,并直接从 UIButton 调用它们。
var i = 0;
function next() {
++i;
window.location.hash = "hl" + i;
return false;
}
function prev() {
if (i > 1)
--i;
window.location.hash = "hl" + i;
return false;
}
$(document).ready(function() {
// get the total number of display sections
// where the ID starts with "display_"
var len = $('div[id^="hl"]').length;
$('#next').click(function() {
next();
});
$('#prev').click(function() {
prev():
});
});
来自 UIButton 的调用将是:
- (IBAction)next:(id)sender {
[animalDesciption stringByEvaluatingJavaScriptFromString:@"next()"];
}
顺便说一句:我认为您忘记在 next()
函数中使用 len
以避免跳过最后一个显示部分。
关于javascript - 使用 UIButton 单击 UIWebView 中的 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10240905/