我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
我需要的信息在一个元标签中。当属性=“视频”时,我如何访问元标签的“内容”数据?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
当前回答
这个代码适用于我
<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/
其他回答
将所有元值复制到缓存对象:
/* <meta name="video" content="some-video"> */
const meta = Array.from(document.querySelectorAll('meta[name]')).reduce((acc, meta) => (
Object.assign(acc, { [meta.name]: meta.content })), {});
console.log(meta.video);
如果你使用JQuery,你可以使用:
$("meta[property='video']").attr('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" />'
在Jquery中,你可以实现这一点:
$("meta[property='video']");
在JavaScript中,你可以通过以下方法实现:
document.getElementsByTagName('meta').item(property='video');
有一个更简单的方法:
document.getElementsByName('name of metatag')[0].getAttribute('content')