是否可以使用 jQuery 来读取元标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1036351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use jQuery to read meta tags
提问by Ankur
Is it possible to use jQuery to read meta tags. If so do you know what the basic structure of the code will be, or have links to any tutorials.
是否可以使用 jQuery 来读取元标记。如果是这样,你知道代码的基本结构是什么,或者有任何教程的链接。
回答by MiffTheFox
Just use something like:
只需使用类似的东西:
var author = $('meta[name=author]').attr("content");
回答by Danilo Moret
Would this parser help you?
这个解析器会帮助你吗?
https://github.com/fiann/jquery.ogp
https://github.com/fiann/jquery.ogp
It parses meta OG data to JSon, so you can just use the data directly. If you prefer, you can read/write them directly using JQuery, of course. For example:
它将元OG数据解析为JSon,因此您可以直接使用数据。如果您愿意,当然可以直接使用 JQuery 读取/写入它们。例如:
$("meta[property='og:title']").attr("content", document.title);
$("meta[property='og:url']").attr("content", location.toString());
Note the single-quotes around the attribute values; this prevents parse errors in jQuery.
注意属性值周围的单引号;这可以防止 jQuery 中的解析错误。
This is the same reply I gave here: https://stackoverflow.com/questions/4059207
这是我在这里给出的相同答复:https: //stackoverflow.com/questions/4059207
回答by charltoons
I just tried this, and this could be a jQuery version-specific error, but
我刚刚试过这个,这可能是一个特定于 jQuery 版本的错误,但是
$("meta[property=twitter:image]").attr("content");
resulted in the following syntax error for me:
导致我出现以下语法错误:
Error: Syntax error, unrecognized expression: meta[property=twitter:image]
Apparently it doesn't like the colon. I was able to fix it by using double and single quotes like this:
显然它不喜欢冒号。我能够通过使用这样的双引号和单引号来修复它:
$("meta[property='twitter:image']").attr("content");
(jQuery version 1.8.3 -- sorry, I would have made this a comment to @Danilo, but it won't let me comment yet)
(jQuery 1.8.3 版——抱歉,我本想对 @Danilo 发表评论,但它不会让我发表评论)
回答by Sauce
jQuery now supports .data();
, so if you have
jQuery 现在支持.data();
,所以如果你有
<div id='author' data-content='stuff!'>
use
用
var author = $('#author').data("content"); // author = 'stuff!'
回答by illvm
$("meta")
Should give you back an array of elements whose tag name is META and then you can iterate over the collection to pick out whatever attributes of the elements you are interested in.
应该给你一个标签名称为 META 的元素数组,然后你可以遍历集合来挑选你感兴趣的元素的任何属性。
回答by PoussinDev
For select twitter meta name , you can add a data attribute.
对于选择 twitter 元名称,您可以添加数据属性。
example :
例子 :
meta name="twitter:card" data-twitterCard="" content=""
$('[data-twitterCard]').attr('content');