随机引用超链接脚本
Random quote hyperlink script?
嗨,我一直在试图弄清楚如何使用随机引用脚本,其中每个引用都是指向不同页面的超链接。到目前为止,这是我用于引号的示例。有没有办法使每个报价成为自己的超链接?
<script language="JavaScript">
var Quotation=new Array() // do not change this!
Quotation[0] = "Test.";
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.";
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
此脚本将自身替换为带有随机引号和相应 href 的 <a>
标签:
<script>
(function(){
var quotes = [
{text: "Test.", href: "http://example.com"},
{text: "Sanity is a golden apple with no shoelaces.", href: "http://example.com"},
{text: "Repent! The end is coming, $9.95 at Amazon.", href: "http://example.com"},
{text: "Honesty blurts where deception sneezes.", href: "http://example.com"},
{text: "Pastry satisfies where art is unavailable.", href: "http://example.com"},
{text: "Delete not, lest you, too, be deleted.", href: "http://example.com"},
{text: "O! Youth! What a pain in the backside.", href: "http://example.com"},
{text: "Wishes are like goldfish with propellors.", href: "http://example.com"},
{text: "Love the river's '"beauty'", but live on a hill.", href: "http://example.com"},
{text: "Invention is the mother of too many useless toys.", href: "http://example.com"}
];
var scripts = document.getElementsByTagName('script');
var script = scripts[scripts.length - 1];
var quote = quotes[Math.floor(Math.random()*quotes.length)];
var a = document.createElement('a');
a.href = quote.href;
a.appendChild(document.createTextNode(quote.text));
script.parentNode.replaceChild(a, script);
})();
</script>
JSFiddle
如果您只想将脚本放在您想要链接的位置并且您只想在页面上放置一次,这是最好的方法。如果您希望在页面上多次使用它,则可以以不同的方式执行此操作,这样您就不需要脚本的多个副本。
相关文章:
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 用超链接替换URLS
- 使用JavaScript从超链接加载时的默认下拉值
- Hammer.js阻止在Android Webview中点击超链接
- 将超链接添加到“;标题“;标记文本
- 文本建议的超链接
- 用Javascript按钮替换Javascript超链接
- HTML中js脚本中的超链接
- 随机引用超链接脚本
- 创建一个超链接(或按钮),用于执行 python 脚本,然后在脚本完成时重定向
- 是否可以使用超链接更改页面并在目标页面上激活脚本
- 超链接在谷歌脚本中不起作用
- Mailto或超链接不会;由于子菜单脚本,无法处理页脚菜单
- 使用php或java脚本或jquery在html页面上禁用所有超链接10秒
- 打开一个方框,以显示内容时,点击超链接使用html/java脚本
- 在iframe脚本中禁用超链接
- Indesign脚本GREP在应用超链接目的地时出错
- jQuery脚本忽略超链接
- 添加javascript变量到脚本内的超链接