由于某些原因,我的页面在 header 中包含两个元标记部分,并且它们是相同的。
如果这个元标记只有一行,我可以创建一个 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;
}
});
关于javascript - 如何使用jquery删除重复的meta标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615384/