当图像和链接是json对象属性的值时,它们不起作用

时间:2016-04-24 04:36:23

标签: javascript html json object hyperlink

我不知道发生了什么,但即使在全球宣布之后,我的图片和链接都无法正常工作!? 如果我将src直接写入src,那么一切似乎都很好,甚至提醒弹出相同的url和img src!需要帮助的人!

var combo0;
var combo1;
var combo2;
var combo3;
var random;
var images=["../IMAGES/park.jpg","../IMAGES/palace.jpg","../IMAGES/oriental.jpg"]
var weburls=["https://www.royalplaza.com.hk","https://www.caesars.com","http://www.venetianmacao.com/"]


function gethotel(){           // a= check-in b=check-out and c=no-of-rooms

    var arraysofobjects=[

        combo0=[

        ],

        combo1=[
            {hotelname:"The Royal Plaza",purl:"images[0]",rflag:"",weburl:"weburls[0]",description:"Locate"
            ,price:"HKD 1200"},
        ],

        combo2=[
            {hotelname:"The Royal Plaza",purl:"images[0]",rflag:"",weburl:"weburls[0]",description:"Located  drive away."
            ,price:"HKD 1200"},
            {hotelname:"Caesars Palace",purl:"images[1]",rflag:"",weburl:"weburls[1]",description:"Built in 1903, the treatments"
            ,price:"HKD 1900"}
        ],

        combo3=[
            {hotelname:"The Royal Plaza",purl:"images[0]",rflag:"",weburl:"weburls[0]",description:"Located in less ."
            ,price:"HKD 1200"},
            {hotelname:"Caesars Palace",purl:"images[1]",rflag:"",weburl:"weburls[1]",description:"Built in 1903, tatments"
            ,price:"HKD 1900"},
            {hotelname:"The Venetian Hotel",purl:"images[2]",rflag:"",weburl:"weburls[2]",description:"The Vor area."
            ,price:"HKD 2200"}
        ]
    ]

     random=Math.floor((Math.random() * 4));
     return arraysofobjects[random];
}

var object=gethotel();
for(var i=0; i<random;++i) {

    var heading = '<h2>' + object[i].hotelname + '</h2>';
    var description = '<p>' + object[i].description + '</p>';
    var priceho='<h4>' + object[i].price + '</h4>';

    //web url
    var a = document.createElement('a');
    var linkText = document.createTextNode("Offical website");
    a.appendChild(linkText);
    a.title = "official website";
    a.href = object[i].weburl;

    var hotel =  '<img src="object[i].purl">' + '<div>'+ heading  + description + priceho + '<a target="_blank" href=object[i].weburl>Official website</a>' +'</div>';
    var str="Details";
    var link=this.str.link("room.html");
    document.getElementById("hotels").innerHTML+=hotel + link;

 }

1 个答案:

答案 0 :(得分:0)

看起来可能是因为您的weburl:"weburls[0]"属性值有引号。试试它:weburl:weburls[0]

我假设你的代码中有一个名为weburls的数组,并且上面的属性假设weburl属性设置为等于weburls中的0索引项。

使用引号表示此行a.href = object[i].weburl;之后,a.href字面上等于"weburls[0]"。您应该能够对DOM进行检查以确认这一点。

<强>更新

for循环中的代码似乎可以替换为:

var heading = '<h2>' + object[i].hotelname + '</h2>';
var description = '<p>' + object[i].description + '</p>';
var priceho='<h4>' + object[i].price + '</h4>';

var hotel =  
    '<img src="' + object[i].purl + '" />' + 
    '<div>' + heading + description + priceho + 
        '<a target="_blank" href="' + object[i].weburl + '">Official website</a>' + 
    '</div>';
document.getElementById("hotels").innerHTML += hotel;