从链接的href属性获取完整URI

编程入门 行业动态 更新时间:2024-10-24 18:19:24
本文介绍了从链接的href属性获取完整URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在某些方面得到确认。

I would like to have a confirmation on some point.

我的目标是在阅读时始终获得相同的字符串(在我的情况下是URI)来自链接的href属性。示例:

My goal is to always get the same string (which is the URI in my case) while reading the href property from a link. Example:

< a href =test.htm/> ,其中base_url = domain.name/

<a href="test.htm" /> with base_url = domain.name/

< ; a href =../ test.htm/> with base_url = http:/ /domain.name/domain/

<a href="../test.htm" /> with base_url = domain.name/domain/

< a href =domain.name/test.htm /> with base_url =来自 domain.name/ 的任何文件夹

<a href="domain.name/test.htm" /> with base_url = any folder from domain.name/

我需要从以上3种情况(或任何其他情况)获得 domain.name/test.htm 相同的字符串)。

I need to get domain.name/test.htm from the 3 situations above (or any other identical string).

经过一些测试后,似乎 my_a_dom_node.href 总是返回完全限定的URI,包括 domaine.name ,这应该可以满足我的需求。

After some tests, it appears that my_a_dom_node.href always return the full-qualified URI, including the domaine.name, which should be okay for what I want.

jQuery有不同的行为, $(my_a_dom_node).attr('href')返回内容(文本)出现在HTML中。所以我的诀窍是使用 $(my_a_dom_node).get(0).href 来获取完整的URI。

jQuery has a different behaviour and $(my_a_dom_node).attr('href') returns the content (text) that appears inside the HTML. So my trick is to use $(my_a_dom_node).get(0).href to get the full URI.

问题是:我能依靠吗?

推荐答案

是的,你可以信赖!

当人们使用简单的javascript(没有jQuery)时,许多人问你要求的相反,他们想要得到href属性中写的真实url而不是完整的url,在这种情况下他们过去只是这样做:

Once when people was using simple javascript (no jQuery) many asked the opposite of what you are asking, they wanted to get the real url as written in the href attribute and not the full one, in such case they used to simply do:

my_a_dom_node.getAttribute('href', 2); //works both IE/FF

然后是jQuery帮助人们不要浪费时间寻找他们需要这样的代码,jQuery总是返回href属性中写的真实url。

Then it came jQuery that helped people not to waste their time in finding out they would need such code and jQuery always returns the real url as written in the href attribute.

有趣的是,现在有人问如何获取完整的URL,因为jQuery返回了href属性中写的那个。

It's funny that now someone is asking how to get the full url because jQuery returns the one written in the href attribute.

更多推荐

从链接的href属性获取完整URI

本文发布于:2023-10-26 09:48:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   完整   链接   URI   href

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!