如何使用 JavaScript 从元标记获取信息?
我需要的信息位于元标记中。当 property="video"
时,如何访问元标记的 "content"
数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
The information I need is in a meta tag. How can I access the "content"
data of the meta tag when property="video"
?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
我的函数变体:
My variant of the function:
这段代码适用于我
JS
示例小提琴: http://jsfiddle.net/muthupandiant/ogfLwdwt/
This code works for me
JS
Example fiddle: http://jsfiddle.net/muthupandiant/ogfLwdwt/
这是一个函数,它将返回任何元标记的内容并记住结果,避免不必要的 DOM 查询。
这是一个扩展版本,它还查询 开放图标记,并使用 Array#some:
Here's a function that will return the content of any meta tag and will memoize the result, avoiding unnecessary querying of the DOM.
And here's an extended version that also queries for open graph tags, and uses Array#some:
更新版本:
update version:
将所有元值复制到缓存对象:
copy all meta values to a cache-object:
我们可以使用直接一行
来获取元描述或关键字或头部分中的任何元标记,如以下代码所示:
只需将 ['description'] 更改为关键字或元名称 rang 的元素
这是一个示例:
使用 document.head 获取元名称值
The simple way preferred
We can use direct one line to get meta description or keyword or any meta tag in head section as this code:
Just change ['description'] to keywords or element of meta name rang
This is an example:
using document.head to get meta names values
如果您对获取所有元标记的更深远的解决方案感兴趣,您可以使用这段代码
If you are interessted in a more far-reaching solution to get all meta tags you could use this piece of code
我个人更喜欢将它们放入一个对象哈希中,然后我可以在任何地方访问它们。这可以很容易地设置为可注入变量,然后所有东西都可以拥有它,并且只抓取一次。
通过包装函数,这也可以作为一个衬垫来完成。
I personally prefer to just get them in one object hash, then I can access them anywhere. This could easily be set to an injectable variable and then everything could have it and it only grabbed once.
By wrapping the function this can also be done as a one liner.
仅供参考,根据 https://developer.mozilla.org/en -US/docs/Web/HTML/Element/meta 全局属性有效,这意味着
id
属性可以与getElementById
一起使用>。FYI according to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta global attributes are valid which means the
id
attribute can be used withgetElementById
.使用
meta
root 然后获取和设置其任何属性:Using
meta
root and then getting and setting any of its properties:复杂网站和元数据的问题在于元标记并不总是具有 itemprop 属性。在某些情况下,它们仅具有 itemprop 属性,但没有 name 属性。
使用此脚本,您可以获取所有具有 itemprop 属性的元并打印其内容。
The problem with complex websites and metadata is that meta tags not always have the itemprop attribute. And in some case they have itemprop only but not a name attribute.
With this script you can get all the Meta with an itemprop attribute and print its content.
使用 vanilla-js 非常简单......
It's very simple with vanilla-js...
演示
Demo
document.head.querySelector('meta[property=video]').content;
document.head.querySelector('meta[property=video]').content;
如果元标记是:
JQuery 将是:
JavaScript 将是:(它将返回整个 HTML)
if the meta tag is:
JQuery will be:
JavaScript will be: (It will return whole HTML)
其他答案可能应该可以解决问题,但是这个更简单并且不需要 jQuery:
最初的问题使用了 带有
property=""
属性的 RDFa 标记。对于普通的 HTML标签,您可以使用如下内容:
The other answers should probably do the trick, but this one is simpler and does not require jQuery:
The original question used an RDFa tag with a
property=""
attribute. For the normal HTML<meta name="" …>
tags you could use something like:你可以使用这个:
You can use this:
这里有一艘班轮
One liner here
有一个更简单的方法:
There is an easier way:
使用方式:
本页示例:
Used in this way:
The example on this page:
如果您使用 JQuery,则可以使用:
If you use JQuery, you can use:
在 Jquery 中,您可以通过以下方式实现此目的:
在 JavaScript 中,您可以通过以下方式实现此目的:
In Jquery you can achieve this with:
In JavaScript you can achieve this with:
这样你就可以获得元的内容。
this way you can get the content of the meta.
方式 - [ 1 ]
您可能会收到错误:
未捕获的 TypeError: Cannot read property 'getAttribute' of null
Way - [ 2 ]
您可能会收到错误:
Uncaught TypeError: Cannot read property 'getAttribute' of null
Way - [ 3 ]
相反,会出现错误,您会得到
null
,那就好。Way - [ 1 ]
You may get error:
Uncaught TypeError: Cannot read property 'getAttribute' of null
Way - [ 2 ]
You may get error:
Uncaught TypeError: Cannot read property 'getAttribute' of null
Way - [ 3 ]
Instead getting error, you get
null
, that is good.简单的一个,对吧?
Simple one, right?