我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?

HTML:

<meta property="video" content="http://video.com/video33353.mp4" />

当前回答

document.head.querySelector(“元[属性=视频]”).content;

其他回答

有一个更简单的方法:

document.getElementsByName('name of metatag')[0].getAttribute('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;
}

如果你使用JQuery,你可以使用:

$("meta[property='video']").attr('content');

供参考,根据https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta全局属性是有效的,这意味着id属性可以与getElementById一起使用。

如果您对获得所有元标记的更深远的解决方案感兴趣,可以使用这段代码

function getAllMetas() {
    var metas = document.getElementsByTagName('meta');
    var summary = [];
    Array.from(metas)
        .forEach((meta) => {
            var tempsum = {};
            var attributes = meta.getAttributeNames();
            attributes.forEach(function(attribute) {
                tempsum[attribute] = meta.getAttribute(attribute);
            });
            summary.push(tempsum);
        });
    return summary;
}

// usage
console.log(getAllMetas());