我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
当前回答
供参考,根据https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta全局属性是有效的,这意味着id属性可以与getElementById一起使用。
其他回答
其他答案应该可以做到这一点,但这一个更简单,不需要jQuery:
document.head.querySelector("[property~=video][content]").content;
最初的问题使用了带有property=""属性的RDFa标记。对于正常的HTML <meta name=""…>标签,您可以使用如下内容:
document.querySelector('meta[name="description"]').content
供参考,根据https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta全局属性是有效的,这意味着id属性可以与getElementById一起使用。
如果元标签是:
<meta name="url" content="www.google.com" />
JQuery将是:
const url = $('meta[name="url"]').attr('content'); // url = 'www.google.com'
JavaScript将是:(它将返回整个HTML)
const metaHtml = document.getElementsByTagName('meta').url // metaHtml = '<meta name="url" content="www.google.com" />'
document.head.querySelector(“元[属性=视频]”).content;
function getDescription() {
var info = document.getElementsByTagName('meta');
return [].filter.call(info, function (val) {
if(val.name === 'description') return val;
})[0].content;
}
版本更新:
function getDesc() {
var desc = document.head.querySelector('meta[name=description]');
return desc ? desc.content : undefined;
}