我正在尝试从该数组中获取一个对象并将其所有值打印到一个 div 中,是否可能
function Car(company, name, price, details, image, alt){
this.company = company;
this.name = name;
this.price = price;
this.details = details;
this.image = image;
this.alt = alt;
}
var carInformation = [new Car("Company: Ferrari", "Name: Marenello", "Price: $250,000", "Details: Fast. Very Fast.", "images/ferrari.jpg","image of ferrari"),
new Car("Company: Dodge", "Name: Viper", "Price: $100,000","Details: Great Acceleration","images/dodge.jpg", "image of viper"),
new Car("Company: Ford", "Name: Shelby", "Price: $80,000", "Details: Muscle Car", "images/mustang.jpg", "image of mustang"),
new Car("Company: Back To The Future", "Name: Delorean", "Price: $20,000", "Details: Travels through time","images/delorean.jpg", "image of delorean"),
new Car("Company: Lamborghini", "Name: Diablo", "Price: $250,000", "Details: Fastest","images/lambo.jpg","image of lamborghini"),
new Car("Company: Mercedes Benz", "Name: SLR", "Price: $180,000", "Details: Classy Vehicle.","images/benz.jpg","image of mercedes benz"),
new Car("Company: Chevrolet", "Name: Corvette", "Price: $70,000", "Details: Fiberglass body Light Vehicle.","images/vette.jpg","image of corvette"),
new Car("Company: Porsche", "Name: Carrera", "Price: $120,000", "Details: Great Handling.","images/porsche.jpg", "image of porsche"),
new Car("Company: Audi", "Name: R8", "Price: $110,000", "Details: Fast and Classy.", "images/audi.jpg","image of audi") ];
for(i=0;i<carInformation.length;i++) {
$('#container').append('<div class="product"><li><a href="#" ><img src="' + carInformation[i].image + '" alt="' + carInformation[i].alt +'" ></img></a></li><div class="description"><p class="carCompany">'+ carInformation[i].company +'</p><p class="carName">'+ carInformation[i].name +'</p><p class="carPrice">'+ carInformation[i].price +'</p><p class="carDetails">'+ carInformation[i].details +'</p></div></div>');
$('#productPage').append('<div id="carInfo">' + carInformation[i] + '</div>');
}
'#container' 行有效。是否可以只打印所有值而不写入每个属性?就像我在“#productPage”中尝试的那样?它返回对象对象。为什么?当我写 console.log(carInformation[0]); 我能够看到对象中的所有值。