简体   繁体   English

如何使用带有锚标记的数组中的随机选择的URL链接

[英]How to use a randomly selected URL link from an array with anchor tag

Before I ask my question I want to give a little bit of background story so we are all on the same page. 在提出问题之前,我想先讲一点背景故事,以便我们都在同一页面上。

So I have an array of 200 items written in an external js file like in this example: 因此,我在外部js文件中编写了一个包含200个项目的数组,如以下示例所示:

var theArray = [
    {"name": "ford", "year": "2004", "url": "www.ford.com"}, 
    {"name": "chevrolet", "year": "2006", "url": "www.chevrolet.com"} 
];

The reason I created my array like this is because I am going to be randomly choosing an index within the array, and that randomly generated number will dictate which information is going to be displayed on the html page, so to be able to use correct information for each car, this is what i did after creating the array: 我之所以这样创建数组,是因为我将随机选择数组中的索引,并且该随机生成的数字将决定将在html页面上显示的信息,以便能够使用正确的信息对于每辆车,这是我在创建数组后所做的事情:

var randomPick = Math.floor(Math.random() * theArray.length);
document.getElementbyId("carnameFord").innerHTML = theArray[randomPick].name; 
document.getElementbyId("caryearFord").innerHTML = theArray[randomPick].year;
document.getElementbyId("carurlFord").innerHTML = theArray[randomPick].url;

Now when I was working on the HTML portion of my little project. 现在,当我在做我的小项目的HTML部分时。

I had no problem displaying the name and year on my html file, which by the way this is the how I displayed them on the html page: 我在html文件上显示名称和年份没有问题,顺便说一下,这就是我在html页面上显示它们的方式:

<h2>Name: <font color="red"><span id="carnameFord"></span></font></h2>
<h2>Year: <font color="red"><span id="caryearFord"></span></font></h2>

NOW, MY QUESTION IS: 现在,我的问题是:

Because I am randomly choosing an index within the array, I can't have a set url to put in href. 由于我是在数组中随机选择一个索引,因此无法在URL中设置href。 So, how can I create a hyperlink which would look like this: "Want to Learn More? CLICK HERE! 因此,如何创建如下所示的超链接:“想了解更多信息?请点击此处!

and when the user clicks on "CLICK HERE!" 当用户点击“点击此处!”时 it basically takes the user to www.ford.com which would be the url being stated in the array. 它基本上会将用户带到www.ford.com,这将是数组中说明的网址。

Edit: 编辑:

Possibly below code can help you with your requirement. 下面的代码可能会帮助您满足您的要求。

 var theArray = [ {"name": "ford", "year": "2004", "url": "www.ford.com"}, {"name": "chevrolet", "year": "2006", "url": "www.chevrolet.com"} ]; const labels = { learnMore: "Want to Learn More?", name: "Name", year: "Year", click: "CLICK HERE!" } const htmlArray = theArray.map((el, index) => { return ` <div> <h2>${labels.name}: <font color="red"><span>${el.name}</span></font></h2> <h2>${labels.year}: <font color="red"><span>${el.year}</span></font></h2> ${labels.learnMore} <a href="${el.url}" title="${el.name}">${labels.click}</a> </div> `; }) document.getElementById("container").innerHTML = htmlArray.join(''); 
 <div id="container"> </div> 

Happy Coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM