javascript - 添加指向随机文本的链接

标签 javascript html

我在下面有一些代码可以加载随机短信。我需要为每条消息添加一个链接,这样当您单击每条消息时,它会转到不同的 html 页面。不知道该怎么做!希望你能帮忙

不确定将代码放在哪里才能让它工作:

每条消息

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--

var r_text = new Array ();
r_text[0] = "I was just thinking about you!";
r_text[1] = "You are a great example for others.";
r_text[2] = "You have great ideas.";
r_text[3] = "When I grow up I want to be you!";
r_text[4] = "I appreciate all of your opinions.";

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'>" +
r_text[i]  + "</FONT></center><br />");

var bgcolorlist=new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];

</script><br> <br>
    <br>
    <br>
    <style type="text/css">
<!--
body,td,th {
     color: #000;
     font-family: Helvetica, sans-serif;
</html>

最佳答案

在 HTML 中添加一个 anchor 标记与消息的关联链接。

var r_text = new Array();
r_text[0] = {
  msg: "I was just thinking about you!",
  link: "http://www.google.com"
};
r_text[1] = {
  msg: "You are a great example for others.",
  link: "http://www.mylink.com"
};
r_text[2] = {
  msg: "You have great ideas.",
  link: "http://www.yourlink.com"
};
r_text[3] = {
  msg: "When I grow up I want to be you!",
  link: "http://www.test.com"
};
r_text[4] = {
  msg: "I appreciate all of your opinions.",
  link: "http://www.facebook.com"
};

var i = Math.floor(r_text.length * Math.random());

document.write("<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='" + r_text[i].link + "'>"+
  r_text[i].msg + "</a></FONT></center><br />");

var bgcolorlist = new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background = bgcolorlist[Math.floor(Math.random() * bgcolorlist.length)];
body,
td,
th {
  color: #000;
  font-family: Helvetica, sans-serif;
}

ES6 版本(稍微重构)

var r_text = new Array();
r_text = [{
    msg: "I was just thinking about you!",
    link: "http://www.google.com"
  },
  {
    msg: "You are a great example for others.",
    link: "http://www.mylink.com"
  },
  {
    msg: "You have great ideas.",
    link: "http://www.yourlink.com"
  }, {
    msg: "When I grow up I want to be you!",
    link: "http://www.test.com"
  }, {
    msg: "I appreciate all of your opinions.",
    link: "http://www.facebook.com"
  }
];

var i = Math.floor(r_text.length * Math.random());

document.write(`<br /><br /><br /><br /><br /><br /><br /><center><FONT SIZE=72><FONT COLOR='#FFFFFF'><a href='${r_text[i].link}'>${r_text[i].msg}</a></FONT></center><br />`);

var bgcolorlist = new Array("#228B22", "#FFD700", "#ADFF2F", "#FF69B4", "#CD5C5C", "#4B0082", "#7CFC00", "#ADD8E6", "#E84643", "#ED0A07", "#EA2907", "#E5294B", "#E00D26", "#FF3030", "#FC7500", "#F95700", "#F43900", "#F95620")

document.body.style.background = bgcolorlist[Math.floor(Math.random() * bgcolorlist.length)];

关于javascript - 添加指向随机文本的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57804820/

相关文章:

javascript - 在数据 spy "affix"内启用滚动

javascript - 如何调整大小然后使用 Canvas 裁剪图像

php - 需要在 php 中将 $_FILES 转换为 $_POST

javascript - 确定对象/嵌入是否被禁止?

css - 边框重叠 float Div

html - css3 在另一个 div 下投影,z-index 不工作

javascript - 使用 Python Flask 自定义 Stripe Checkout

javascript - 类型 'Subscription' 不可分配给类型 'Observable<Exercise[]>'

javascript - 按类型和名称定位输入字段和提交按钮

javascript - 从我的网页引用 D3.js 库