2012-04-20 30 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