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

HTML:

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

当前回答

复杂网站和元数据的问题是元标签并不总是有itemprop属性。在某些情况下,它们只有itemprop而没有name属性。

使用这个脚本,您可以获得带有itemprop属性的所有元数据并打印其内容。

  const allMeta = document.getElementsByTagName("meta");
    
  for (let i = 0; i < allMeta .length; i++) {
      if( allMeta [i].getAttribute("itemprop") != null){
        console.log( allMeta [i].getAttribute("itemprop")+":"+allMeta [i].getAttribute('content') );
      }
  }

其他回答

这个代码适用于我

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

很简单,对吧?

document.head.querySelector("meta[property=video]").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;
}

使用元根,然后获取和设置它的任何属性:

let meta = document.getElementsByTagName('meta')

console.log(meta.video.content)
> "http://video.com/video33353.mp4"

meta.video.content = "https://www.example.com/newlink"

有一个更简单的方法:

document.getElementsByName('name of metatag')[0].getAttribute('content')