Javascript 将favicon附加到类名
我有这个剧本:Javascript 将favicon附加到类名,javascript,jquery,url,favicon,Javascript,Jquery,Url,Favicon,我有这个剧本: 它允许显示特定url对应的favicon。正如您所看到的,favicon被附加到url中,但是我需要它附加到class=“class”中,我如何才能做到这一点?我在代码中看不到任何可以更改的内容。我已经修改了脚本,将“config”var包含在“target”属性中。这个“目标”只不过是一个jQuery选择器(在本例中是.class),默认为“This” 因此: 我希望这对你有用 这是完整的工作代码: <!DOCTYPE html> <html> <
它允许显示特定url对应的favicon。正如您所看到的,favicon被附加到url中,但是我需要它附加到
class=“class”
中,我如何才能做到这一点?我在代码中看不到任何可以更改的内容。我已经修改了脚本,将“config”var包含在“target”属性中。这个“目标”只不过是一个jQuery选择器(在本例中是.class),默认为“This”
因此:
我希望这对你有用
这是完整的工作代码:
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
<script type="text/javascript">
jQuery.fn.favicons = function (conf) {
var config = jQuery.extend({
insert: 'appendTo',
target: this
}, conf);
return this.each(function () {
jQuery('a[href^="http://"]', this).each(function () {
var link = jQuery(this);
var faviconURL = link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') + '/favicon.ico';
var faviconIMG = jQuery('<img src="' + '" alt="" />')[config.insert]($(config.target));
var extImg = new Image();
extImg.src = faviconURL;
if (extImg.complete) {
faviconIMG.attr('src', faviconURL);
}
else {
extImg.onload = function () {
faviconIMG.attr('src', faviconURL);
};
}
});
});
};
$(function(){
jQuery('#hello').favicons({insert: 'insertBefore', target: '.class' });
});
</script>
</head>
<body>
<p id="hello"><a href="http://www.google.nl/">Google</a></p>
<p class="class">apend favicon over her instead</p>
</body>
</html>
JS-Bin
文章、旁白、数字、页脚、页眉、H组、,
菜单,导航,部分{显示:块;}
jQuery.fn.favicons=函数(conf){
var config=jQuery.extend({
插入:“附件”,
目标:这个
},conf);
返回此。每个(函数(){
jQuery('a[href^=“http://”],this.)。每个(函数(){
var link=jQuery(this);
var faviconURL=link.attr('href')。替换(/^(http:\/\/[^\/]+).$/,“$1')+”/favicon.ico';
var faviconIMG=jQuery(“”)[config.insert]($(config.target));
var extImg=新图像();
extImg.src=faviconURL;
如果(extImg.complete){
faviconIMG.attr('src',faviconURL);
}
否则{
extImg.onload=函数(){
faviconIMG.attr('src',faviconURL);
};
}
});
});
};
$(函数(){
jQuery('#hello').favicons({insert:'insertBefore',target:'.class'});
});
取而代之的是阿本德·法维康
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
<script type="text/javascript">
jQuery.fn.favicons = function (conf) {
var config = jQuery.extend({
insert: 'appendTo',
target: this
}, conf);
return this.each(function () {
jQuery('a[href^="http://"]', this).each(function () {
var link = jQuery(this);
var faviconURL = link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') + '/favicon.ico';
var faviconIMG = jQuery('<img src="' + '" alt="" />')[config.insert]($(config.target));
var extImg = new Image();
extImg.src = faviconURL;
if (extImg.complete) {
faviconIMG.attr('src', faviconURL);
}
else {
extImg.onload = function () {
faviconIMG.attr('src', faviconURL);
};
}
});
});
};
$(function(){
jQuery('#hello').favicons({insert: 'insertBefore', target: '.class' });
});
</script>
</head>
<body>
<p id="hello"><a href="http://www.google.nl/">Google</a></p>
<p class="class">apend favicon over her instead</p>
</body>
</html>