javascript Jquery 更改元标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19100560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:21:09  来源:igfitidea点击:

Jquery to change meta tag

javascriptjquerymeta-tags

提问by user2687646

I am trying to make this meta tag always have the current URL of the page it is on.

我试图使这个元标记始终具有它所在页面的当前 URL。

<meta property="og:url" content="http://shop.famsf.org/do/product/BK5160" />

How would I go about changing a meta tag with JS or JQ?

我将如何使用 JS 或 JQ 更改元标记?

回答by user2687646

Changing Facebook meta tag Content Attribute using JQuery

使用 JQuery 更改 Facebook 元标记内容属性

It is definitely possible to change meta tags using jQuery, but I don't think that's going to accomplish what you want since the meta tags are pulled by the FB scraper. The FB scraper goes out and scrapes your page to read the meta tags.

绝对可以使用 jQuery 更改元标记,但我认为这不会实现您想要的,因为元标记是由 FB 刮板拉取的。FB 抓取器会启动并抓取您的页面以读取元标记。

Editing Meta Tags

You can update the attributes of your page by updating your page's tags. Note that og:title and og:type are only editable initially - after your page receives 50 likes the title becomes fixed, and after your page receives 10,000 likes the type becomes fixed. These properties are fixed to avoid surprising users who have liked the page already. Changing the title or type tags after these limits are reached does nothing, your page retains the original title and type.

For the changes to be reflected on Facebook, you must force your page to be scraped. The page is scraped when an admin for the page clicks the Like button or when the URL is entered into the Facebook URL Linter. You can programmatically force your page to be scraped by cURL'ing the linter. For example ...

编辑元标签

您可以通过更新页面的标签来更新页面的属性。请注意 og:title 和 og:type 最初只能编辑 - 在您的页面收到 50 个赞后,标题变为固定,而在您的页面收到 10,000 个赞后,类型变为固定。这些属性是固定的,以避免让已经喜欢该页面的用户感到惊讶。达到这些限制后更改标题或类型标签没有任何作用,您的页面保留原始标题和类型。

要使更改反映在 Facebook 上,您必须强制抓取您的页面。当页面管理员单击“赞”按钮或将 URL 输入到 Facebook URL Linter 时,页面会被抓取。您可以通过 cURL'ing linter 以编程方式强制抓取您的页面。例如 ...

https://developers.facebook.com/docs/opengraph/

https://developers.facebook.com/docs/opengraph/

BUT if you want to do this:

但是如果你想这样做:

$('meta[property=og\:url]').attr('content', window.location.href);

回答by gilly3

Select the element using tagname and attribute. Set the value using .attr():

使用标记名和属性选择元素。使用.attr()以下方法设置值:

$("meta[property='og:url']").attr("content", location.href);

回答by Shreedhar

You just grab a meta tags with the name 'og:url' and set its content attribute like this :

您只需获取名称为 'og:url' 的元标记并像这样设置其内容属性:

$('meta[property="og:url"]').attr('content', window.location.href);

回答by Jo?o Oliveira

Property not working i used like that:

属性不起作用我这样使用:

$("meta[name='og:url']").attr("content", location.href);