我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
当前回答
如果元标签是:
<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" />'
其他回答
很简单,对吧?
document.head.querySelector("meta[property=video]").content
如果你使用JQuery,你可以使用:
$("meta[property='video']").attr('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.querySelector('meta[property="video"]').content
这样你就可以得到元数据的内容。
我的函数变体:
const getMetaValue = (name) => {
const element = document.querySelector(`meta[name="${name}"]`)
return element?.getAttribute('content')
}