English 中文(简体)
Wait for getJSON response
原标题:

i m trying to make a script using Yahoo s YQL in a web applicaion. The script looks over a text paragraph and then it finds some references and appends on the text as a popup. It all goes fine until I have to loop through each reference with class "verso", after getting my response it appends all the references in the last element of the list.

Here is the javascript code (rewrited and simplified)

            var url="";
            var version = "RVR1960";
            var verso = "";

            $("div.post-body").find(".verso").each(function(i){
                $resultado = null;
                verso = $(this).text();
                url = "http://query.yahooapis.com/v1/public/yql?" + "q=select%20*%20from%20html%20where%20url%3D%22"+
                    encodeURIComponent("http://www.biblegateway.com/passage/?search=" + verso +
                    "&version=" + version) + "%22&format=xml &callback=?";
                $pasaje = $(this);

                $.getJSON(url,function(data){
                    if(data.results[0])
                    {
                        $resultado = null;
                        $resultado = $(data.results[0]).find("div.result-text-style-normal:first");
                        $resultado.find("h5, div, a").remove();
                        $("<div class= cita ><span class= left >&ldquo;</span>"+
                            $resultado.html()+
                            "<p align= right ><b>"+verso+"</b></p>"+
                            "<span class= right >&rdquo;</span></div>").appendTo($pasaje);
                    }
                    else
                    {
                        $resultado = $("<p>Pasaje no encontrado.</p>");
                    }
                });
            });

and here is my html:

<div class="post-body"><b class="verso">Juan 3:16</b><b class="verso">Mateo 11:28</b><b class="verso">Juan 1:1</b></div>

I ll appreciate all the help

最佳回答

so, I have change several things in your code, now it works, have fun;)

var url="";
var version = "RVR1960";
var verso = "";

$("div.post-body > b.verso").each(function(i){
    resultado = null;
    verso = $(this).text();
    url = "http://query.yahooapis.com/v1/public/yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent("http://www.biblegateway.com/passage/?search="+verso+"&version="+version)+"%22&format=xml &callback=?";

    $.getJSON(url,function(data){
        if(data.results[0])
        {
            resultado = null;
            resultado = $(data.results[0]).find("div.result-text-style-normal:first");
            resultado.find("h5, div, a").remove();
            $("<div class= cita ><span class= left >&ldquo;</span>"+resultado.html()+"<p align= right ><b>"+verso+"</b></p>"+"<span class= right >&rdquo;</span></div>").appendTo($("div.post-body > b.verso")[i]);
        }
        else
        {
            resultado = $("<p>Pasaje no encontrado.</p>");
        }
    });
});
问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签