单击具有 UIButton 的 UIWebView 中的锚点

Click an anchor in a UIWebView with a UIButton

本文关键字:UIWebView 单击 UIButton      更新时间:2023-09-26

我的网页上有一个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 时的锚点点击:

- (IBAction)next:(id)sender {
    [animalDesciption stringByEvaluatingJavaScriptFromString:@"document.getElementById('"next'").click();"];
}

但这行不通!它在 HTML 页面上效果很好,只需单击标识为"next"的锚点即可。

任何想法为什么在单击按钮时不起作用?

顺便说一句,我可以使用当前的设置调用像myFunc()这样的标准 JavaScript 函数,但它不会做这样的事情!

任何想法将不胜感激!

你可以为下一个和上一个实现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以避免跨过最后一个显示部分。