我在下面有一些代码可以加载随机短信。我需要为每条消息添加一个链接,这样当您单击每条消息时,它会转到不同的 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/