Javascript selectQuery是否可以将变量作为id名称

时间:2016-04-23 09:05:44

标签: javascript html css

我想在selectQuery中使用变量作为id名称,但是我失败了。 Hotelno是id名称的变量。我怎么能修复它所以我可以将该变量用作id名称?

var hotel_1 = {
    'name':"Hullett House",'url':"http://www.hulletthouse.com/",'flag':true,'photo':"../image/0.png",  
    'des':"Hullett House is perfectly located for both business and leisure guests in Hong Kong.",
    'price':"HK$1800"   };



var hotel_2 = {
    'name':"Heritage Lodge",'url':"https://www.royalplaza.com.hk",'flag':false,'photo':"../image/14831652.jpg",
    'des':"Heritage Lodge is located in the Western District. ",
    'price':"HK$1120"   };



var hotel_3 = {
    'name':"Royal Plaza",'url':"https://www.royalplaza.com.hk",'flag':false,'photo':"../image/44228962.jpg",
    'des':"Royal Plaza Hong Kong is located very close to shopping malls, trains and markets. ",
    'price':"HK$2000"   };



var hotel = [hotel_1,hotel_2,hotel_3];

function getHotel() {
var noHotel=Math.floor(Math.random()*3);
for (var i = 0; i<noHotel; i++) {
var hotelno = 'hotel'+ (i+1);

document.getElementById(hotelno).style.visibility="visible";
document.querySelector('#hotel1 .title').innerHTML=hotel[i].name;   //can work
document.querySelector('#hotel1 img').src= hotel[i].photo; //can work
document.querySelector('"#"+hotelno .line').innerHTML=hotel[i].des;  //can not work

}}

0 个答案:

没有答案