使用 JQuery 更改 Facebook 元标记内容属性
我想知道如何使用 JQuery 来替换 Facebook 元标记的内容属性。
我搜索了 StackOverflow,发现了这个例子:
$("meta[property=og:title]").attr("content", title);
$("meta[property=og:url]").attr("content", url);
在我的代码中,我有以下元标记:
<meta property="og:title" content="some content"/>
<meta property="og:url" content="some content"/>
<meta property="og:site_name" content="some content"/>
当我运行应该更改“title”和“url”变量内容的函数时,更改不会发生地方。 “内容”的相同值仍然保持不变。
我想知道如果可能的话,如何使用 JQuery 更改 Facebook 元标记的值。
I want to know how can I use JQuery in order to replace the content attribute of my Facebook meta tags.
I searched StackOverflow and what I've found was this example:
$("meta[property=og:title]").attr("content", title);
$("meta[property=og:url]").attr("content", url);
In my code I have the following meta tags:
<meta property="og:title" content="some content"/>
<meta property="og:url" content="some content"/>
<meta property="og:site_name" content="some content"/>
When I run the function that should change the content for the 'title' and 'url' variables, the change doesn't take place. The same values for the 'content' still remains the same.
I want to know how, if it's possible, to use JQuery to change the value of Facebook's meta tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是它对我的作用:
注意双反斜杠,没有它 Jquery 将失败并出现错误。
This is how it worked for me:
note the double backslash, without it Jquery will fail with an error.
使用 jQuery 更改元标记绝对是可能的,但我认为这不会实现您想要的效果,因为元标记是由 FB 抓取工具拉取的。 FB 抓取工具会抓取您的页面以读取元标记。
https://developers.facebook.com/docs/opengraph/
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.
https://developers.facebook.com/docs/opengraph/
Facebook 抓取工具不执行 JavaScript。
因此,使用 Javascript 修改元标签以显示不同的共享内容是行不通的。
您需要使用服务器端代码根据需要将它们写在 HTML 源代码中。
The Facebook scraper doesn't execute JavaScript.
So using Javascript to modify the metatags in order to show different share content won't work.
You'd need to use serverside code to write them out as needed in the HTML source.