.js 配列から製品をロードするページを作成しようとしています:
var products= new Array( );
products.push( {name: 'product01', photo: 'tshirt1', title: 'One Man Wolf Pack', price: '14.99', description: 'All Wolf Packs start with one'} );
products.push( {name: 'product02', photo: 'tshirt2', title: 'Bear Arms', price: '14.99', description: 'Know your rights'} );
products.push( {name: 'product03', photo: 'tshirt3', title: 'Accuracy', price: '19.99', description: 'The opposite of a fallacy'} );
products.push( {name: 'product04', photo: 'tshirt4', title: 'HUMA', price: '19.99', description: 'A twist on sports'} );
products.push( {name: 'product05', photo: 'tshirt5', title: 'Illuminati', price: '19.99', description: 'You went, now get the t-shirt'} );
function get_product( name ) {
for( var x = 0 ; x < products.length ; x++ ) {
if( products[ x ].name == name ) {
return products[ x ];
}
}
return null;
}
For ループを使用して各項目をステップ実行し、製品の単純なレイアウトを作成しようとしています。
window.onload = function ( ) {
load_page( );
}
function create_cookie( x, name1 ) {
//if( this.onclick ){
document.cookie = "details" + x + "=" + name1;
//}
}
function load_page ( ){
var holder = document.getElementById( "details" );
for( var x = 0 ; x < products.length ; x++ ) {
var li = document.createElement( "li" );
var name1 = products[ x ].name;
var href1 = document.createElement( "a" );
href1.href = "details.html";
href1.id = products[ x ].name
var image1 = document.createElement( "img" );
image1.src = products[ x ].photo + ".jpg" ;
var title1 = document.createTextNode( products[ x ].title );
var bk1 = document.createElement( "br" );
var bk2 = document.createElement( "br" );
var hor = document.createElement( "hr" );
href1.appendChild( image1 );
li.appendChild ( href1) ;
li.appendChild( title1 );
holder.appendChild( li );
href1.onclick = create_cookie( x, name1 );
alert(document.cookie);
}
}
onclick イベントで問題が発生すると思います。「クリック」イベントをチェックする「If ステートメント」がない場合、すべての Cookie が自動的に読み込まれます。
おそらく、間違ってコーディングされたボタンを持っているのでしょう。
ボタンを使用してこれを適切に作成し、Cookie を作成する方法についての洞察をいただければ幸いです。
ありがとう!
〜アンドレアス