javascript - 使用 UIButton 单击 UIWebView 中的 anchor

标签 javascript jquery iphone objective-c xcode

我的网页上有一个 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/

相关文章:

iphone - 为模拟器构建 OpenEars 时出现重复符号错误

iphone - 适用于 iOS 的 SQLite - fts 表中的重音符(代字号)不敏感匹配

javascript - 将 Express 后端数据从数据库发送到 React 前端

javascript - 由于 JS 返回 false,链接不可点击

javascript - 当电子邮件字段未填写时显示 Bootstrap 警报

javascript - 使用 jquery 在 svg 中选择图像

iphone - 自动布局中的 ScrollView

javascript - 引用错误:函数未定义

javascript - 以编程方式模拟存储事件

javascript - 从动态 HTML 元素检索 jQuery 数据返回未定义