0

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

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

2 回答 2

1
function showQuotation(){
    if(Quotation[whichQuotation].substr(0,7)=='http://' || Quotation[whichQuotation].substr(0,8)=='https://')
    document.write('<a href="'+Quotation[whichQuotation]+'">'+Quotation[whichQuotation]+'</a>');
    else
    document.write(Quotation[whichQuotation]);
}
于 2012-04-16T00:08:50.000 回答
1

这就是我要做的:

  1. 假设报价以 http(s):// 开头
  2. 在 ShowQuotation 中:使用正则表达式来识别此类字符串。可能是不必要的复杂。检查长度为 7 的子字符串是否为http://或长度为 8 的子字符串为https://,假设其余部分都是 URL 的一部分
  3. 使其成为链接
于 2012-04-15T23:07:44.587 回答