我正在阅读“JavaScript 缺失手册”,并且有一个脚本会自动在此链接旁边添加链接的 URL。
$('a[href^="http://"]').each(function(){
var href = $(this).attr('href');
href = href.replace('http://', '');
$['a'].after(' (' + href + ') ');
}); // end each
问题是它根本不起作用。有人可以解释一下这段代码有什么问题吗?
最佳答案
更改此行
$['a'].after(' (' + href + ') ');
至
$('a').after(' (' + href + ') ');
$ 应该作为函数调用,而不是数组。
虽然查看你的代码,但你可能想要这个:
$(this).after(' (' + href + ') ');
<小时/>
编辑:
完整代码如下:
$('a[href^="http://"]').each(function(){
var href = $(this).attr('href');
href = href.replace('http://', '');
$(this).after(' (' + href + ') ');
}); // end each
关于javascript - 在链接文本旁边添加指向网站外部的链接 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925308/