men*_*ith 2 html javascript jquery attributes selector
我在 HTML 代码中有以下内容:
<meta name="citation_journal_title" content="Psychological Bulletin" />
Run Code Online (Sandbox Code Playgroud)
使用以下方法很容易获取内容:
document.getElementsByName("citation_journal_title")[0].getAttribute("content")
Run Code Online (Sandbox Code Playgroud)
但是,我无法处理这个:
<meta property="og:site_name" content="APA PsycNET" />
Run Code Online (Sandbox Code Playgroud)
你如何检索 的内容og:site_name
?我知道
如何使用 javascript 从元标记中获取信息?
但我正在寻找一些非常简单的东西
document.getElementsByName("citation_journal_title")[0].getAttribute("content")
Run Code Online (Sandbox Code Playgroud)
您需要使用属性选择器[attr=value]
来完成这项工作。querySelector()
像这样使用它
var attr = document.querySelector("meta[property='og:site_name']").getAttribute("content");
console.log(attr);
Run Code Online (Sandbox Code Playgroud)
<meta property="og:site_name" content="APA PsycNET" />
Run Code Online (Sandbox Code Playgroud)