2017-06-05 7 views
-2

私は約15人のアーティストを含むアーティスト(artists.html)ページを作成しました。今私は各アーティストのプロファイルを1つのHTMLファイルで動的に作成しようとしています。そこで私は別のHTMLファイル(single-artist.html)を作成し、訪問者がクリックしたアーティストのプロフィールを表示します。クリック時にJSONファイルからデータを動的に取得する方法は?

artists.html(15組のアーティストのうち、以下の2つの例である)

<div class="all-artist" id="artists"> 
      <div class="col-md-4 col-sm-6 col-xs-6 artist-single" data-number="0"> 
       <div> 
        <a href="artist-single.html"> 
         <img src="img/1.jpg" alt=""> 
        </a> 
       </div> 
      </div> 

      <div class="col-md-4 col-sm-6 col-xs-6 artist-single" data-number="1"> 
       <div> 
        <a href="artist-single.html"> 
         <img src="img/2.jpg" alt=""> 
        </a> 
       </div> 
      </div> 

    </div> 

[{ 

"name": "Arsh", 
"logo": "img/logo.jpg", 
"bio": "Arsh is a singer/songwriter" 

}, 

{ 

"name": "Benz", 
"logo": "img/logo.jpg", 
"bio": "Benz is a singer" 

}] 

メインアーティストsingle.html

<div id="name"></div> 
<div id="logo"></div> 
<div id="bio"></div> 

data.json。 js

$('#all_artists artist-single').on('click',function(){ 
window.location.href="artist-single.html"; 
var num = $(this).data('number'); 
$.getJSON('data.json',function(data) { 
    $('#name').html(data[num].name); 
    $('#description').html(data[num].bio); 
    $('#logo').append('<img src=data[num].logo>'); 
    }); 
}); 

アーティスト画像をクリックするとartist-single.htmlにリダイレクトされますが、値を取得していません。同じイベントで値をリダイレクトして取得している可能性があります。ヘルプは本当に感謝しています。

+1

リダイレクト後に実行されるコードは中止されます。その部分は、他のページの文書準備機能で実行する必要があります。 – madebydavid

+0

新しいページが読み込まれるまで待つ必要があります。 https://stackoverflow.com/questions/15589401/changing-window-location-then-waiting-for-the-window-to-load –

答えて

-1
$(document).ready(function() { 
    var num = $.cookie('num'); 

    $.getJSON('data.json', function(data) { 
    $('#name').html(data[num].name); 
    $('#description').html(data[num].bio); 
    $('#logo').append('<img src=data[num].logo>'); 
    }); 

    $('#artists .artist-single').on('click', function() { 
    var num = $(this).data('number'); 
    $.cookie('num', num); 
    window.location.href = "artist-single.html"; 
    }); 

}); 
+0

ウィリアム、答えに感謝しますが、私はまだ動作していないのではないかと心配しています。 –

関連する問題