如何获得“原始"JavaScript 中的 href 内容 [英] How to get "raw" href contents in JavaScript
问题描述
我正在尝试编写一个 GreaseMonkey 脚本,我想在其中找到所有相对链接的链接.在我看来,这样做的方法是将 href 的内容与 /^https?:///
进行匹配.
I am trying to write a GreaseMonkey script in which I want to find all of the links that are relative links. It seemed to me that the way to do that would be to match the contents of href against /^https?:///
.
但我发现当我访问锚点的 href 属性时,它总是被规范化或被加工成包含http"的形式.也就是说,如果 HTML 包含:
But I find that when I access the anchor's href attribute, it's always normalized or cooked into a form that contains "http". That is, if the HTML contains:
<a id="rel" href="/relative/link">inner</a>
访问
document.getElementById("rel").href
返回
http://example.com/relative/link
如何访问 href 属性中的原始数据?
How can I access the raw data in the href attribute?
或者,有没有更好的方法来查找相关链接?
Alternately, is there a better way to find relative links?
推荐答案
试试getAttribute
方法代替.
Try the getAttribute
method instead.
这篇关于如何获得“原始"JavaScript 中的 href 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!