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

HTML:

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

当前回答

document.querySelector('meta[property="video"]').content

这样你就可以得到元数据的内容。

其他回答

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

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());

这个代码适用于我

<meta name="text" property="text" content="This is text" />
<meta name="video" property="text" content="http://video.com/video33353.mp4" />

JS

var x = document.getElementsByTagName("META");
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        if (x[i].name=="video")
        {
             alert(x[i].content);
         }

    }    

示例:http://jsfiddle.net/muthupandiant/ogfLwdwt/

其他答案应该可以做到这一点,但这一个更简单,不需要jQuery:

document.head.querySelector("[property~=video][content]").content;

最初的问题使用了带有property=""属性的RDFa标记。对于正常的HTML <meta name=""…>标签,您可以使用如下内容:

document.querySelector('meta[name="description"]').content

在Jquery中,你可以实现这一点:

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

在JavaScript中,你可以通过以下方法实现:

document.getElementsByTagName('meta').item(property='video');

如果元标签是:

<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" />'