【问题标题】:How do I insert a hyperlink into a Javascript randomizing text array?如何将超链接插入到 Javascript 随机化文本数组中?
【发布时间】:2014-04-14 18:40:07
【问题描述】:

代码如下:

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

function text() {
};

text = new text();
number = 0;

// textArray        
text[number++] = "I read the other day some verses written by an eminent painter which were original and not conventional."         
text[number++] = "The soul always hears an admonition in such lines, let the subject be what it may."        
text[number++] = "The sentiment they instil is of more value than any thought they may contain."        
text[number++] = "To believe your own thought, to believe that what is in your private heart is true for all men, that is genius." 

increment = Math.floor(Math.random() * number);

document.write(text[increment]);

//--></SCRIPT>

具体是我想要的话:

对你来说是真的

从数组中的第四个文本链接到一些外部网站。

&lt;a&gt; 标签似乎对我不起作用。字符串方法也没有。当然,我是个十足的笨蛋。

【问题讨论】:

  • 您有语法错误(未转义的引号)。如果那是 HTML,它也不是封闭引用或封闭标记。您可以编写 Array literal 而不是在此处创建自定义构造函数。最后,document.write 是个坏主意。
  • 这是我正在使用的 HTML 的一部分,我从 Javascript Source 借来的。我假设代码是紧密引用/紧密标记的,尽管我太新手无法确定。我也不知道“数组文字”和“document.write”是什么,或者它们是做什么的。 (笨蛋,你看)。这些都是让我原来的问题无法回答的事情吗?
  • 你必须从某个地方开始......

标签: javascript arrays hyperlink random-sample


【解决方案1】:

我猜你的问题是你在使用 a 标签时插入的引号,你应该使用正确的引号,如下所示:

text[number++] = "I read the other day some verses written by an eminent painter which were original and not conventional"         
text[number++] = "The soul always hears an admonition in such lines, let the subject be what it may."        
text[number++] = "The sentiment they instil is of more value than any thought they may contain."
text[number++] = "To believe your own thought, to believe that what is in your private heart is <a href='javascript:void(0);'>true for </a> all men, that is genius." 

http://jsbin.com/luyod/2/

【讨论】:

  • 我对将整个报价链接到其他网站不感兴趣,只需几句话。具体来说,我希望第四条引文中的“对你来说是真实的”字样链接到外部网站。
【解决方案2】:

让我们一步一步来,

  1. 创建一个名为text 的新variable,并让它成为一个数组

    var text = [];
    

    这里的[] 是一个数组字面量

  2. 用您的字符串

    填充数组 text
    text.push("I read the other day some verses written by an eminent painter which were original and not conventional.");
    text.push("The soul always hears an admonition in such lines, let the subject be what it may.");
    text.push("The sentiment they instil is of more value than any thought they may contain.");
    text.push("To believe your own thought, to believe that what is in your private heart is true for all men, that is genius.";
    

    这里,someArray.push 是一种将新的 item 添加到 Array someArray 的方法。每个新项目都有自己的索引,索引从 0 开始。

  3. 数组中选择一个随机索引text并将其分配给一个新的variable,i

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

    JavaScript 中,方法 Math.random 返回一个值严格小于 1大于或等于 0 .这意味着我们可以将值乘以 Array 的项目数(或 length),然后向下取整(或 floor)从 Array 中选择随机索引的数字。

  4. index itextString 中,将任何出现的 "true for you" 替换为您的链接。让我们将其命名为 variable 名称 str,以便我们可以参考它。

    var str = text[i].replace(/true for you/g, "<a href=\"http://stackoverflow.com\">true for you</a>");
    

    这里,/true for you/gregular expressionRegExp。末尾的g 代表global,意思是“一直这样做,直到你到达终点”。

  5. 以您喜欢的方式将这个新的字符串(我们称之为str)写入您的#document。由于我们看不到与您的 HTML 标记相关的任何内容,我会保留您的 document.write,但这被认为是 bad practice,因此您将来可能想了解替代方案.

    document.write(str);
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多