如何使用jQuery获取<link>标签的href?

Mat*_*att 2 jquery hyperlink css-selectors jquery-selectors link-tag

我已经看到很多关于如何使用标记执行此操作,并且使用该标记执行此操作没有任何问题,但我似乎无法检索标记的href属性<link>.

甚至试图抓住链接标签:

alert($("link").length);
Run Code Online (Sandbox Code Playgroud)

给0.

有任何想法吗?

谢谢,
马特

mac*_*ca1 5

如果$("link").length返回0,你的选择器没有提出任何东西,根本没有希望获得链接.

你需要一个更好的选择器.也许通过ID(#linkId)或特定类等攻击它.你可以使用大量的选择器:

http://api.jquery.com/category/selectors/

你的第一步是让.length显示1.然后从那里.attr('href')将为您提供链接位置.

编辑:对不起,误解了你的目的.我以为你的意思是锚标签.以下是我成功访问链接标记的方法:

var links = window.document.getElementsByTagName('link');
$(links).each(function() {
    $(this).attr('href')  // this is your href for the link tag in the loop
});
Run Code Online (Sandbox Code Playgroud)