[英]How to add hyperlink in array (Javascript)
I have a random quote generator script, and I need to add hyperlinks for each quote. 我有一个随机引用生成器脚本,我需要为每个引用添加超链接。 The issue - I can't figure out how to accomplish this for the life of me.
问题-我无法弄清楚如何一生完成这项工作。
I'm a novice in javascript but after searching around, thinking there's an easy solution to my problem, I can't find a workable answer to this. 我是javascript的新手,但在四处搜寻之后,认为我的问题有一个简单的解决方案,因此找不到可行的答案。
How do I go about adding a hyperlink in an array? 如何在数组中添加超链接? I'd appreciate this.
我很欣赏这一点。 It's probably so simple too.
这也可能是如此简单。
Here's the page to the random quote generator, and I posted the code below. 这是随机报价生成器的页面,我在下面发布了代码。 Thank you.
谢谢。 https://www.hscripts.com/scripts/JavaScript/random-quote-generator.php
https://www.hscripts.com/scripts/JavaScript/random-quote-generator.php
I posted the code below as well. 我也在下面发布了代码。
<style>
.row {
padding-left: 10px;
background-color: white;
font-family: verdana, san-serif;
font-size: 13px;
}
</style>
<!-- Script by hscripts.com -->
<script type="text/javascript">
var arr = new Array();
arr.push("Javascript is different from Java");
arr.push("Javascript is different from Java");
arr.push("Javascript is different from Java");
arr.push("CSS - Cascading Style Sheet");
arr.push("HTML is a platform independent language");
function rotate() {
var num = Math.round(Math.random() * 3);
add(num);
}
function add(i) {
var chi = document.createTextNode(arr[i]);
var tab1 = document.getElementById("add1");
while (tab1.hasChildNodes()) {
tab1.removeChild(tab1.firstChild);
}
tab1.appendChild(chi);
}
</script>
<!-- Script by hscripts.com -->
<table align=center style="background-color:#C0C0C0">
<tr>
<td background-color:#c0c0c0 align=center width=300 style="font-family:Times New Roman;">
<b>Random Quote Generator</b>
</td>
</tr>
<tr>
<td id=add1 class=row width=300 align=center>Click Next to Display Random message</td>
</tr>
<tr>
<td align=center>
<input type=button value="Next" border=0 onclick="rotate()">
</td>
</tr>
</table>
You can keep html code in your array eg 您可以将html代码保留在数组中,例如
arr.push('<a href="http://google.pl">CSS</a>');
But I don't prefer mix html code with js. 但是我不喜欢将html代码与js混合使用。 Look at my solution on JSFiddle https://jsfiddle.net/xoL2bbtd/
在JSFiddle上查看我的解决方案https://jsfiddle.net/xoL2bbtd/
I little modified your array and add function 我稍微修改了数组并添加了功能
function add(i) {
var chi = document.createElement('a');
chi.textContent = arr[i].text;
chi.setAttribute('href', arr[i].link);
var tab1 = document.getElementById("add1");
if (tab1.hasChildNodes()) {
tab1.removeChild(tab1.firstChild);
}
tab1.appendChild(chi);
}
I create anchor element and set href attribute. 我创建锚元素并设置href属性。 In array I keep object which contains text and link property
在数组中,我保留包含文本和链接属性的对象
And one more thing. 还有一件事。 Create array by using new Array is slower than using [].
使用new Array创建数组比使用[]慢。 Check this https://jsperf.com/new-array-vs-literal/15
检查此https://jsperf.com/new-array-vs-literal/15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.