javascript - 如何使该脚本中的引文成为可点击链接?

标签 javascript

我有来自 codelifter 的这个脚本,它运行得很好。 我希望如果下面的引用包含一个网址,那么它将变得可点击。 任何帮助都会很棒。 谢谢

<script language="JavaScript">
// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array.  Remember
// to increment the Quotation[x] index!

Quotation[0] = "http://www.youtube.com/watch?v=zH6U5y086hw";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

最佳答案

这就是我要做的:

  1. 假设引用以 http(s)://开头
  2. 在 ShowQuotation 中:使用正则表达式来识别此类字符串。 可能不必要地复杂。检查 length-7 子字符串是否为 http:// 或 length-8 子字符串是否为 https:// ,假设其余部分都是 URL 的一部分
  3. 将其设为链接

关于javascript - 如何使该脚本中的引文成为可点击链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166938/

相关文章:

javascript - 我想在单击一个 div 时隐藏所有其他 div

javascript - Angular 超时不起作用

javascript - JS : difference between undefined and `undefined`

javascript - 二维旋转矩形碰撞

javascript - 如果浏览器无法显示带 alpha channel 的 webm,则后备图像

javascript - jQuery 成功处理程序未在生产服务器上执行,但在开发中运行良好

javascript - Jquery 从 FormData 对象中删除值

javascript - 如何使用 SignalR 处理移动设备上的断开连接事件?

javascript - FancyBox 2.x 中显示/隐藏标题不可靠

类似 JavaScript 浏览器的 GET 请求