[英]In IE 7, how can you get the literal value of a link’s href attribute, if the link was created using jQuery?
If you have a regular link in HTML, you can get the value of its href attribute using jQuery's attr
function: 如果您有HTML中的常规链接,则可以使用jQuery的
attr
函数获取其href属性的值:
<a id="testLink" href="test/link.html">Test Link</a>`
>>> $('#testLink').attr('href');
testLink.html
Example: 例:
However, if the link was created using jQuery, then in IE 7 , this function returns the absolute URL that the browser would access if you clicked on the link (eg https://stackoverflow.com/questions/ask/testLink.html ), instead of the literal value of the href
attribute. 但是,如果链接是使用jQuery创建的,则在IE 7中 ,此函数将返回单击链接后浏览器将访问的绝对URL(例如https://stackoverflow.com/questions/ask/testLink.html ) ,而不是
href
属性的文字值。
Example: 例:
I've also tried this
, this.href
, and this.getAttribute('href')
, and they all return an absolute URL. 我也尝试过
this
, this.href
和this.href
this.getAttribute('href')
,它们都返回一个绝对URL。
Is there any way to get the literal value of the href
attribute of a link created by jQuery in IE 7? 有什么方法可以获取IE 7中jQuery创建的链接的
href
属性的文字值吗?
If you are creating the link in a way that jQuery is forced to use .innerHTML
, it will work not properly and is documented in: http://api.jquery.com/html/ (also http://api.jquery.com/jQuery/#creating-new-elements ) : 如果要创建在jQuery是强制使用的方式链接
.innerHTML
,它将工作不正常和中记载: http://api.jquery.com/html/ (也HTTP://api.jquery。 com / jQuery /#creating-new-elements ):
This method uses the browser's innerHTML property.
此方法使用浏览器的innerHTML属性。 Some browsers may not generate a DOM that exactly replicates the HTML source provided.
某些浏览器可能不会生成完全复制提供的HTML源代码的DOM。 For example, Internet Explorer prior to version 8 will convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.
例如,版本8之前的Internet Explorer会将链接上的所有href属性转换为绝对URL,而版本9之前的Internet Explorer在没有添加单独的兼容性层的情况下将无法正确处理HTML5元素。
To fix it, create the link in a way that doesn't force jQuery to use .innerHTML
: 要解决此问题,请以不强制jQuery使用
.innerHTML
的方式创建链接:
$('#test').append( $("<a>", {href: "test/link.html", text: "Test Link"}));
I think it's not possible, at least not in IE7. 我认为这是不可能的,至少在IE7中是不可能的。 JQuery uses
innerHTML
, and IE seems to rewrite the href attribute to a Fully Qualified Domain Name (FQDN) on rendering (earlier versions of IE are known to do so). JQuery使用
innerHTML
,并且IE似乎在呈现时将href属性重写为完全合格域名(FQDN)(已知IE的早期版本可以这样做)。 So the literal value is lost on rendering. 因此,文字值在渲染时会丢失。 If you know the path, you could try to find it using some string eqation, stripping the domain from the url or something.
如果您知道路径,则可以尝试使用一些字符串方程式找到它,从URL或其他内容中删除域。 Or forget about IE<8 alltogether if possible.
或者,如果可能的话,完全不要考虑IE <8。
May be this article sheds some light 可能是本文有所启发
If you use plain javascript and DOM-methods to add elements, you can retrieve the literal value even in IE7. 如果使用普通的javascript和DOM方法添加元素, 则即使在IE7中也可以检索文字值。 See this jsfiddle
看到这个jsfiddle
try this: 尝试这个:
$('a').click(function () {
var hrf = this.href;
alert('this.href: \n' + hrf.slice(hrf.lastIndexOf('/')+1, hrf.length) );
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.