简体   繁体   中英

How to get the content of the content attribute for a meta tag with js/jquery

I'm trying to get the content of the content attribute of a meta tag on a webpage, I want my code to find all the metatags on a given webpage and I want the code to then read what's inside the content attribute and return that as a readable string. However, I don't want to specify any name or property attributes for the meta tag because I want the code to pick up all the content of all the meta tags.

I started like this:

var meta = $(document).find("meta[content]").text();

But this just returns an empty value, probably because it can't really find the content because I haven't specified it well. I'm new to this, please help me! :D

Thanks and may the force be with you!

类似于下面的代码来获取metatag内容

var author = $('meta[name=author]').attr("content");

The text() method returns the text content within an element. This is not correct when trying to retrieve information from a meta element, as they have no text.

instead, to retrieve the value of the content attribute, use attr() :

var content = $('meta[content]').attr('content');

The HTML is as below

<head>
  <meta id="dirName" name="dirName" content="project name" />
</head>

The Jquery code will be

var dirPath= $('#dirName').attr('content');
console.log(dirPath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM