我正在尝试创建一个从 .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 正确创建此内容的见解将不胜感激。
谢谢!
~安德烈亚斯