我想在某些方面得到确认。
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
发布评论