如何从带有JavaScript的元标记中获取信息?

sup*_*lle 112 html javascript greasemonkey meta-tags

我需要的信息是元标记.如何"content"在何时访问元标记的数据property="video"

HTML:

<meta property="video" content="http://video.com/video33353.mp4" />
Run Code Online (Sandbox Code Playgroud)

joe*_*pio 173

其他答案可能应该做的伎俩,但这个更简单,不需要jQuery:

document.head.querySelector("[property~=video][content]").content;
Run Code Online (Sandbox Code Playgroud)

  • 简单,优雅,没有依赖性.比接受的答案更好imo (12认同)
  • 为了使用OG标签,可以像这样添加引号:var title = document.head.querySelector('[property ="og:title"]'); (9认同)
  • 即使我的meta在<head>标签中,`document.head.querySelector`给了我`null`但是`document.querySelector`完美地工作了 (6认同)

Sak*_*ket 113

你可以用这个:

function getMeta(metaName) {
  const metas = document.getElementsByTagName('meta');

  for (let i = 0; i < metas.length; i++) {
    if (metas[i].getAttribute('name') === metaName) {
      return metas[i].getAttribute('content');
    }
  }

  return '';
}

console.log(getMeta('video'));
Run Code Online (Sandbox Code Playgroud)

  • 如果你可以使用querySelector,你可以这样做:`document.querySelector("meta [property ='og:url']").getAttribute('content')` (15认同)
  • 你真正想要的是'让'让它们在本地定义;) (6认同)
  • 我想这个答案是不相关的,你应该使用http://stackoverflow.com/questions/7524585/how-do-i-get-the-information-from-a-meta-tag-with-javascript/35649085 #35649085 (3认同)
  • 感谢您的帮助,但我无法正常工作。 (2认同)
  • 对于只有一个元属性,为什么要循环多次?它可能有数百个元标记,或者可能需要多次获取元值。 (2认同)

Ced*_*Ced 86

这里有很多难以理解的答案.这里有一个班轮

document.querySelector("meta[property='og:image']").getAttribute("content");
Run Code Online (Sandbox Code Playgroud)


小智 22

有一种更简单的方法:

document.getElementsByName('name of metatag')[0].getAttribute('content')
Run Code Online (Sandbox Code Playgroud)

  • `document.querySelector` 版本一直适用于 IE8,所以足够了 (2认同)

小智 16

function getMetaContentByName(name,content){
   var content = (content==null)?'content':content;
   return document.querySelector("meta[name='"+name+"']").getAttribute(content);
}
Run Code Online (Sandbox Code Playgroud)

以这种方式使用:

getMetaContentByName("video");
Run Code Online (Sandbox Code Playgroud)

此页面上的示例:

getMetaContentByName("twitter:domain");
Run Code Online (Sandbox Code Playgroud)


Eli*_*ant 13

$("meta[property='video']").attr('content');
Run Code Online (Sandbox Code Playgroud)

  • 假设jquery,或某些库; 不是javascript (9认同)

小智 11

在Jquery中,你可以通过以下方式实现:

$("meta[property='video']");
Run Code Online (Sandbox Code Playgroud)

在JavaScript中,您可以通过以下方式实现:

document.getElementsByTagName('meta').item(property='video');
Run Code Online (Sandbox Code Playgroud)

  • 这似乎有效(至少在chrome中):`document.getElementsByTagName('meta')['video'].getAttribute('content');`如果标记如下:`<meta name ="video"content = "http://video.com/video33353.mp4"/>` (9认同)
  • 看起来像"item(property ='video');" 部分不起作用 (4认同)

Ahm*_*jar 9

document.querySelector('meta[property="video"]').content
Run Code Online (Sandbox Code Playgroud)

这样你就可以获得元的内容。


*_*лов 5

方式- [ 1 ]

function getMetaContent(property, name){
    return document.head.querySelector("["+property+"="+name+"]").content;
}
console.log(getMetaContent('name', 'csrf-token'));
Run Code Online (Sandbox Code Playgroud)

您可能会收到错误消息:Uncaught TypeError: Cannot read property 'getAttribute' of null


方式- [ 2 ]

function getMetaContent(name){
    return document.getElementsByTagName('meta')[name].getAttribute("content");
}
console.log(getMetaContent('csrf-token'));
Run Code Online (Sandbox Code Playgroud)

您可能会收到错误消息:Uncaught TypeError: Cannot read property 'getAttribute' of null


方式- [ 3 ]

function getMetaContent(name){
    name = document.getElementsByTagName('meta')[name];
    if(name != undefined){
        name = name.getAttribute("content");
        if(name != undefined){
            return name;
        }
    }
    return null;
}
console.log(getMetaContent('csrf-token'));
Run Code Online (Sandbox Code Playgroud)

而不是得到错误,你得到null,那很好。


Eri*_*dal 5

简单的一个,对吧?

document.head.querySelector("meta[property=video]").content
Run Code Online (Sandbox Code Playgroud)