Pergunta

Recentemente, criei uma função XML Feed -> HTML JavaScript para um aplicativo para iPhone que estou desenvolvendo no JQTouch. Original Tutorial & Code.

Eu queria saber se alguém saberia uma maneira rápida e fácil de atualizar os dados XML (pegue o feed novamente) quando um link for clicado.

por exemplo.No código:

<div id="btns">
 <ul>
  <li><a href="#feed">Go to feed</a></li> <!-- When i click this, I want the getDataFeed function to dump the data & rerun. -->
</div>
<div id="feed">
 <div id="content_from_feed_inserted_here"></div>
</div>

No JavaScript:

$(document).ready(function() {

function getDataFeed() {

    $('.loadingPic').show(); // show progress bar

    $.get('http://xxxxxxx.com/information.xml', function(d) {

    $(d).find('location').each(function(){

        var $location = $(this); 
        var the_data = $location.find('xml_data').text();

        var collected_data = collected_data += '<span>' + the_data + '</span>' ;
        $('#content_from_feed_inserted_here').empty().append($(collected_data)); // empty div first

        $('.loadingPic').fadeOut(1400); // remove progress bar
    });

}

// run on page load
getDataFeed();

// how do i also get it running when i click <li><a href="#feed">Go to feed</a></li>

});

Muito obrigado antecipadamente pela sua ajuda!

Foi útil?

Solução

Teste isto: Mover função getDataFeed () {..} fora da função pronta e capture o evento de clique em Link. set id = "feed" em <a>.

function getDataFeed() {

    $('.loadingPic').show(); // show progress bar

    $.get('http://xxxxxxx.com/information.xml', function(d) {

    $(d).find('location').each(function(){

        var $location = $(this); 
        var the_data = $location.find('xml_data').text();

        var collected_data = collected_data += '<span>' + the_data + '</span>' ;
        $('#content_from_feed_inserted_here').empty().append($(collected_data)); // empty div first

        $('.loadingPic').fadeOut(1400); // remove progress bar
    });
}

$(document).ready(function() {

    // how do i also get it running when i click <li><a href="#feed" id="#feed">Go to feed</a></li>
    $('#feed').click(getDataFeed);

    // run on page load
    getDataFeed();

});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top