如何从纯javascript中的< link标记获取属性? [英] How to get attribute from a <link tag in pure javascript?
本文介绍了如何从纯javascript中的< link标记获取属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.blogger.com/rsd.g?blogID=7487871339000666216" />
如何在不使用任何JavaScript库的情况下获取此链接标记的href属性?
How can I get the href attribute of this link tag without using any javascript library?
现在我正在使用下面的代码来实现这个目标,但我想知道是否有更简单的解决方案。
Right now I'm using the code below to achieve this but I want to know whether there is a simpler solution.
var links = document.getElementsByTagName("link");
for (i in links){
var title = links[i].getAttribute("title");
if ( title == "RSD"){
var href = links[i].getAttribute("href");
break;
}
}
推荐答案
现代浏览器支持 querySelector()和 querySelectorAll():
document.querySelector("link[title=RSD]").getAttribute("href");
参见浏览器支持图表。
这篇关于如何从纯javascript中的< link标记获取属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文