简体   繁体   English

添加指向随机文本的链接

[英]Add a Link to random text

I have some code which is below which loads up random text messages.我有一些代码在下面加载随机文本消息。 I need to add a link to each message so when you click on each message it goes to a different html page.我需要为每条消息添加一个链接,以便当您单击每条消息时,它会转到不同的 html 页面。 Not sure how to do this!不知道如何做到这一点! hope you can help希望你能帮忙

not sure where to put the code to get it to work:不知道把代码放在哪里才能让它工作:

for each message对于每条消息

<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>

Add an anchor tag in the HTML with associated link of the message.在 HTML 中添加一个带有消息关联链接的锚标记。

 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; }

An ES6 version ( a little bit refactored ) 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)];

Add a before the text:在文本前添加a

<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'><a href='url'>" +
r_text[i]  + "</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)];

 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.forEach(function(item){ document.write("<a href='" + item.link + "'>"+item.msg + "</a>"); document.write("</br></br>"); });

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

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