在JavaScript中获取元数据属性

编程入门 行业动态 更新时间:2024-10-25 08:28:12
本文介绍了在JavaScript中获取元数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在从元标记中检索信息时遇到麻烦.我正在尝试从网站获取img src,但还不太清楚.这是我正在尝试做的一个例子.

I am having trouble retrieving information from a meta tag. I am trying to get an img src from a website and can't quite figure it out. Here is an example of what I am trying to do.

<meta property="og:image" content="foo.jpg"> var image = document.querySelector('meta[property="og:image"]').getAttribute('content');

我已经尝试过了,但是没有用.有什么想法吗?

I have tried this but it doesn't work. Any ideas?

推荐答案

meta元素并不特殊,您可以通过常规方式查询它们并获取其属性.

meta elements aren't special, you can query for them and get their attributes in the normal way.

在这种情况下,这是从第一个meta[property="og:image"]元素中获取content属性值的方法:

In this case, here's how you'd get the content attribute value from the first meta[property="og:image"] element:

var element = document.querySelector('meta[property~="og:image"]'); var content = element && element.getAttribute("content");

所有现代浏览器以及IE8都支持

querySelector.

querySelector is supported by all modern browsers, and also IE8.

更多推荐

在JavaScript中获取元数据属性

本文发布于:2023-11-12 11:44:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   数据   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!