Get meta data attribute in javascript

假如想象 提交于 2019-12-03 13:40:44

问题


I am having trouble retrieving information from a meta tag. I am trying to get an img src from a website and can't quite figure it out. Here is an example of what I am trying to do.

<meta property="og:image" content="http://foo.jpg">
var image = document.querySelector('meta[property="og:image"]').getAttribute('content');

I have tried this but it doesn't work. Any ideas?


回答1:


meta elements aren't special, you can query for them and get their attributes in the normal way.

In this case, here's how you'd get the content attribute value from the first meta[property="og:image"] element:

var element = document.querySelector('meta[property~="og:image"]');
var content = element && element.getAttribute("content");

querySelector is supported by all modern browsers, and also IE8.



来源:https://stackoverflow.com/questions/29124579/get-meta-data-attribute-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!