简体   繁体   English

jQuery用img src填充meta attr'content'

[英]Jquery fill meta attr 'content' with img src

I have a facebook og tag... 我有一个Facebook og标签...

<meta property="og:image" content="whatever.jpg"/>

And I'd like to change the whatever.jpg to a image on the page contained in a div with an id #ok 我想将what.jpg更改为ID为#ok的div中包含的页面上的图像

<div id="ok">
<img src="saveme.jpg" />
</div>

so i'd end up with 所以我最终会

<meta property="og:image" content="saveme.jpg"/>

and ideas? 和想法? been searching aroundthe api and havent found anything which does this :( 一直在api周围搜索,还没有发现任何可以做到这一点的东西:

This will work for you 这对你有用

 $('meta[property="og:image"]').attr('content',$('#ok img').attr('src'));

An example is here http://jsfiddle.net/RMd27/ 示例在这里http://jsfiddle.net/RMd27/

假设您只有一个带有og:image属性的meta标记:

$('meta[property="og:image"]').attr('content',$('#ok').find('img').attr('src'));

尝试:

$('meta[property="og:image"]').attr("content",$("#ok > img").attr("src"))

You shouldn't be setting any OpenGraph data via JavaScript, since it tends to be crawled by search engines and social networking sites, and those don't have JavaScript (in other words, what they see will be the unaltered value). 您不应该通过JavaScript设置任何OpenGraph数据,因为它往往会被搜索引擎和社交网站抓取,而那些没有JavaScript的数据(换句话说,他们看到的将是不变的值)。 Depending on how you're setting the OpenGraph values, you might want to change your PHP to output the correct image that you want for that particular page. 根据您如何设置OpenGraph值,您可能想要更改PHP以输出特定页面所需的正确图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM