由于某些原因,我的页面的标题中包含两个meta标签部分,它们是相同的。
如果此meta标签只有一行,我可以创建一个jquery代码来删除重复的代码;但是,我的情况是这些元标记包含6行,如以下代码的第一部分所示:
(第二部分是我要删除的重复部分)
<meta property="fb:app_id" content="123456789">
<meta property="og:site_name" content="MyWebsite">
<meta property="og:url" content="http://ThisIsMyWebsite.com">
<meta property="og:title" content="Duplicated Meta Tag Testing">
<meta property="og:description" content="Test the duplicated meta tags">
<meta property="og:image" content="http://ThisIsMyWebsite.com/show.jpg">
<meta property="fb:app_id" content="123456789">
<meta property="og:site_name" content="MyWebsite">
<meta property="og:url" content="http://ThisIsMyWebsite.com">
<meta property="og:title" content="Duplicated Meta Tag Testing">
<meta property="og:description" content="Test the duplicated meta tags">
<meta property="og:image" content="http://ThisIsMyWebsite.com/show.jpg">
是否有找到/删除重复零件的明确方法?(仅保留一个零件)
谢谢。
最佳答案
这是一个解决方案,将仅保留第一个解决方案:
var found = {};
$('meta').each(function(){
var $this = $(this);
if(found[$this.attr('property')]){
$this.remove();
}
else{
found[$this.attr('property')] = true;
}
});