如何获得“原始”效果JavaScript 中的 href 内容
我正在尝试编写一个 GreaseMonkey 脚本,在其中我想找到所有相对链接的链接。在我看来,这样做的方法是将 href 的内容与 /^https?:///
进行匹配。
但我发现当我访问锚点的 href 属性时,它总是被标准化或烘焙成包含“http”的形式。也就是说,如果 HTML 包含:
<a id="rel" href="/relative/link">inner</a>
访问
document.getElementById("rel").href
返回
http://example.com/relative/link
如何访问 href 属性中的原始数据?
或者,是否有更好的方法来查找相关链接?
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?:///
.
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>
accessing
document.getElementById("rel").href
returns
http://example.com/relative/link
How can I access the raw data in the href attribute?
Alternately, is there a better way to find relative links?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请尝试使用
getAttribute
方法。Try the
getAttribute
method instead.典型的。我在发布问题后几乎立即就自己弄清楚了。
而不是:
使用:
当然,我输入这个答案所花费的时间比其他人回答它所花费的时间还要长。 (该死,你们的速度太快了。)
Typical. I figured it out myself almost immediately after posting the question.
instead of:
use:
Of course, it took me longer to type in this answer than it took everyone else to answer it. (Damn, you people are fast.)
这是您可以运行来测试的代码片段。
假设您位于
http://localhost:4200
,这是您在问题中显示的文档。该锚点的
href
属性值为:锚点的
href
值为:希望对您有所帮助。
Here's a code snippet you could run to test.
Let's say, you are at
http://localhost:4200
, and this is your document as you have shown in the question.This anchor's attribute value of
href
is:And anchor's
href
value is:I hope it helps.
获取链接 dom 并为其添加属性,并将实际链接附加到其上。
希望这适用于为 js / ts 下的每个动态页面附加的动态链接。
Get the link dom and add attributes for same and append the actual link to same.
Hope this will work for dynamic links that to append for each dynamic pages under js / ts.