2012-04-20 53 views
0

我有我的网页上的JavaScript函数,我在显示一个UIWebView:在一个UIWebView点击锚与一个UIButton

$(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();"]; 
} 

但这不起作用! 只需点击具有“next”标识的锚点,它就可以在HTML页面上很好地工作。

任何想法,为什么这不起作用,当点击按钮?

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

任何想法将不胜感激!

回答

2

您可以实现下一个和上一个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()"]; 
} 

顺便说一句:我想你忘了使用lennext()功能,避免跨过最后显示部分。

+0

这工作很好! – 2012-04-20 19:28:45