1

jquery accordion 内の and ステートメントのループを作成する方法があるので、JSON を反復処理できます。

以下のコードでは JSON が機能しているため、JSON からのデータで jquery アコーディオンを埋める必要があります。アコーディオンに必要な h3 タグと div タグを回避する方法がわかりません。

         <html>



<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<head>

<link rel="stylesheet" href="jquery-ui-1.8.23.custom/development-bundle/themes/base/jquery.ui.all.css">
    <script src="jquery-ui-1.8.23.custom/development-bundle/jquery-1.8.0.js"></script>
    <script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.core.js"></script>
    <script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.widget.js"></script>
    <script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.mouse.js"></script>
    <script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.selectable.js"></script>
    <script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.accordion.js"></script>
    <link rel="stylesheet" href="jquery-ui-1.8.23.custom/development-bundle/demos/demos.css">
    <script type='text/javascript' src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>

<script>
    $(function() {
        $( "#accordion" ).accordion();
    });
</script><script>
    $(document).ready(function(){
            /* call the php that has the php array which is json_encoded */
            $.getJSON(ReturnPlacesJSON.php, function(data) {
                /* data will hold the php array as a javascript object */
                $.each(data, function(key, val) {
                    var latlng = new google.maps.LatLng(val.xcoord, val.ycoord);
                    var title = (val.title)?val.title:""
                    var icon = 'http://shanewmiller.com/Specials/images/beermug.png';
                    var special = val.Description;
                    var end = (val.endtime)?val.endtime:""
                    var start = (val.starttime)?val.starttime+" - ":""
                    var day = (val.day)?val.day:""
                    var html = val.name + val.address + special + day + start + end;

                    $('<h3 />').html(special).appendTo('#accordion');
                    $('<div />').html(val.name + ' ' + val.address).appendTo('#accordion');
                });
            });
        }); 

    </script>


</head>
<body>

<div id="accordion">
    </div>


</body></html>
4

1 に答える 1

1

あなたがやろうとしているのは、アコーディオン div 内に h3 と div を作成することです。何$('#accordion > h3').each()$('#accordion > div').each()行うかは、既にアコーディオン内にある h3 と div を反復処理することです。

実際に行う必要があるのは、json を反復処理することです。呼び出しによって実行します。$.each(data, ...)次に、アイテムごとに、そのコンテンツで新しい h3 と新しい div を作成します。次のような jQuery を使用して要素を作成します。

$('<h3 />').html(special).appendTo('#accordion');
$('<div />').html(val.name + ' ' + val.address).appendTo('#accordion');

残りのコードは微調整する必要があると思います。たとえば、 を呼び出すかどうかはわかりませんaccordion()。その後、div に入力すると、目的の効果が得られます。

于 2012-12-06T19:37:06.617 に答える