-2

puts.php (JSON)

 {
    "image":[
       {
          "name":'<div id="yes">Hi!</div>'
       }
    ]
 }

进程.php

<HTML>
    <HEAD>
        <TITLE>Process</TITLE>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js"></script>
        <script>
            function loadPage() {

                $(document).ready(function () {

                    $.getJSON('puts.php', function (JSON) {
                        $('#result').empty();

                        $.each(JSON.image, function (i, img) {
                            $('#result').append(img.name)
                        });
                    });
                });
            }

            function loadEfect() {

                $(document).ready(function () {

                    $('#yes').fadeOut();
                    $('#yes').fadeIn();
                });
            }

            function start() {

                loadPage();
                loadEfect();
            }
        </script>
    </HEAD>

    <BODY onLoad="start()">
        <div id="result"></div>
    </BODY>

</HTML>

process.php函数中的代码loadEfect()不起作用。( fadeIn, fadeOutnot working) 页面加载loadPage()功能可以工作,但是loadEfect()功能不工作。为什么是这样?我在哪里可以犯错误?请帮忙。

4

3 回答 3

3

loadEfect isn't working because there's no element with the ID yes.

Also, don't use both onload and $(document).ready(. Only use one method.

function loadPage() {
    $.getJSON('puts.php', function (JSON) {
        $('#result').empty();
        $.each(JSON.image, function (i, img) {
            $('#result').append(img.name)
        });
    });
}

function loadEfect() {
    $('#yes').fadeOut();
    $('#yes').fadeIn();
}

$(document).ready(function(){
    loadPage();
    loadEfect();
}

Then remove onLoad="start()" from your <body> tag.

于 2012-06-05T19:24:59.757 回答
2

它不起作用,因为onload发生在onready. 尝试这样的事情:

$(function(){
    $.getJSON('puts.php' function(json){
        $.each(json.image, function(i, img){
            $('#result').append(img.name)
        };
    });

    $('#yes').fadeOut();
    $('#yes').fadeIn(); 
});
于 2012-06-05T19:22:24.937 回答
1

Only use double-quoted stings in your JSON:

{
    "image": [
        {
            "name": "<div id='yes'>Hi!</div>"
        }
    ]
}

Single-quoted strings are not allowed. See JSONLint.

于 2012-06-05T19:23:27.797 回答