如何使用JavaScript从Blogger获取expr:content
值?
<meta expr:content='data:blog.blogId' itemprop='blogId'/>
<meta expr:content='data:post.id' itemprop='postId'/>
我尝试了getElementsByTagName("meta")
var x = document.getElementsByTagName("meta");
以及querySelectorAll()
var metas = document.querySelectorAll("meta[expr\\:content]");
for (i = 0; i < metas.length; i++) {
console.log(metas[i].getAttribute("expr:content"))}
txt=x.expr:content;
但是我没有收到任何结果,我希望有两个接收结果的变量
x=number of blogId
y=number of postId
答案 0 :(得分:0)
尝试
var blogId = document.querySelector("meta[itemprop='blogId']").getAttribute('expr:content'),
postId = document.querySelector("meta[itemprop='postId']").getAttribute('expr:content');
console.log(postId);
console.log(blogId);
答案 1 :(得分:0)
试试这个:
var metas = document.getElementsByTagName("meta"),
metaData = {}, i, item;
for (i = 0; i < metas.length; i++) {
item = metas[i].getAttribute("itemprop");
if (item == "blogId" || item == "postId"){
metaData[item] = metas[i].getAttribute("expr:content");
}
}
console.log(metaData); // this should give you an object with needed data
答案 2 :(得分:0)
将以下脚本放在&lt; b:includable id =&#39; post&#39;之后。 VAR =&#39;后&#39;&GT;
<script>
var myBlogId = '<data:blog.blogId/>'
var myPostId = '<data:post.id/>'
</script>