我需要的信息在一个meta标签中。当property="video"
时,我如何访问meta标签中"content"
数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
我需要的信息在一个meta标签中。当property="video"
时,我如何访问meta标签中"content"
数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
据https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/meta所述,全局属性是有效的,这意味着id
属性可以与getElementById
一起使用。
var author = document.querySelector("meta[name=author]").content;
alert(`My author is ${author}`);
<html>
<head>
<meta property="video" content="http://video.com/video33353.mp4" />
<meta name="video" content="http://video.com/video33353.mp4" />
</head>
<body>
<script>
var meta = document.getElementsByTagName("meta");
size = meta.length;
for(var i=0; i<size; i++) {
if (meta[i].getAttribute("property") === "video") {
alert(meta[i].getAttribute("content"));
}
}
meta = document.getElementsByTagName("meta")["video"].getAttribute("content");
alert(meta);
</script>
</body>
</html>
document.head.querySelector('meta[property=video]').content;
<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" />'
name
属性,而不是property
属性。使用标准属性的开发者需要根据大多数答案提供的代码进行调整。 - Jens Bannmann