Domanda

Ho un div, che nel documento pronto aggiungo di jQuery - utilizzando la sintassi $("#div id").append('html text') -. Con 10 o giù di lì più div elementi figlio

una volta fatto questo cerco di controllare il contenuto dei div bambino via alert($(".classname")); e ritorna con:

function(a){if(c.isFunction(a))return this.each(function(b){var d=c(this);d.text(a.call(this,b,d.text()))});if(typeof a!=="object"&&a!==w)return this.empty().append((this[0]&&this[0].ownerDocument||s).createTextNode(a));return c.text(this)}

mi sarei aspettato che allerta con i contenuti html dei div bambino e non javascript?

script completo:

<script type="text/javascript">
    $(document).ready(function(){

        // twitter api's base url
        var url="http://search.twitter.com/search.json?callback=?&result_type=recent&q=";
        // we'll store the search term here
        var query = "blah";
        // get the json file
        $.getJSON(url+query,function(json){
            // this is where we can loop through the results in the json object
            $.each(json.results,function(i,tweet){
                // this is where we do what we want with each tweet
                $("#results").append('<div class="tweetBox"><span class="unseen">'+tweet.created_at+'</span><div class="tweetImg"><img src="'+tweet.profile_image_url+'" width="48" height="48" /><a class="overbox" href="http://twitter.com/'+tweet.from_user+'/status/'+tweet.id+'"></a></div>'+tweet.text+' ...said '+((new Date().getTime()/1000/60)-(new Date(tweet.created_at))/1000/60).toFixed(0)+' minutes ago</div>');

            });
        });

        $("#results").height(function(){return $(window).height()-204;});
        alert($(".unseen").html());     
    });
</script>

<div id="results"></div>

Aggiornamento: sicuramente un qualche tipo di jquery / race condition javascript succedendo qui, se si sostituisce la segnalazione setTimeout(function(){alert($(".unseen").html());},1000); restituisce il testo previsto. Se cambio la pausa timeout a 1 millisecondo ritorna null ancora una volta.

Non sono sicuro di una soluzione 'reale' per questo altro che attaccare in ritardo?

È stato utile?

Soluzione

chiamate Ajax (come $.getJSON) sono fatto in modo asincrono.

Ciò significa che, quando la selezione jQuery è fatto (in fondo dello script) la risposta potrebbe non essere ancora ricevuto (ancora in cammino).

Dovete spostare tutto il codice che dipende da elementi creati dalla risposta nella funzione di callback (subito dopo il $.each(...);)

Esempio:

$.getJSON(url+query,function(json){
    // this is where we can loop through the results in the json object
    $.each(json.results,function(i,tweet){
        // this is where we do what we want with each tweet
        $("#results").append('<p>this element is created only when the callback is triggered</p>');
    });
    // do stuff here with all your created elements
    alert('found '+$(".unseen").length+' objects');
});

Si noti inoltre che le html () restituisce la funzione i contenuti html di solo il primo elemento del set .

EDIT: il vostro trucco timeout funziona perché dà l'Ajax chiamata il tempo di cui ha bisogno per completare e attivare la funzione di callback che gli annunci gli oggetti DOM

.

Vedere esempio di lavoro qui .

Altri suggerimenti

Prova

alert($('.classname').html());

Non capisco il motivo per cui è tornato il testo funzione; Sei sicuro che si è appena conclusa $('.classname') nella chiamata alert()? Forse hai avuto $('.classname').html senza il () finale?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top