JQuery 在 IE 中无法正确解析 attr("href") [英] JQuery Not Parsing Properly attr("href") in IE

查看:28
本文介绍了JQuery 在 IE 中无法正确解析 attr("href")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的问题,希望有人能解释一下.我正在使用 Jquery 从另一个网站(我拥有)检索 Http 响应.收到 DOM 后,我会对其进行解析以获取某些信息.但是,当我尝试获取链接的 href 属性时,IE 正在将本地域添加到 href 的开头!

I have a really wierd issue that I'm hoping someone can shed some light on. I am using Jquery to retrieve an Http Response from another website (which I own). Once I receive the DOM, I parse through it in order to get certain information. However, when I try to get the href attribute of a link, IE is adding the local domain to the beginning of the href!

这是我的代码:

$.ajax({ 
                    type: "POST",
                    url: "MyPage.aspx/GetWebResponse",
                    data: "http://myWebSite/pages/AnotherPage.aspx",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    asynch: false,
                    success: function(data)
                    {
                        var __htmlForMainPage = data.d;                   


                        var PageLink = $(__htmlForMainPage).find("a:contains('Get This Link')").attr("href"); 
                                                }   
                });

我的变量 PageLink 应该是/pages/getThisPage.aspx?id=8347".但是,它被返回为 "http://myserver/pages/getThisPage.aspx?id=8347".

My variable PageLink SHOULD be "/pages/getThisPage.aspx?id=8347". However, it is being returned as "http://myserver/pages/getThisPage.aspx?id=8347".

这仅在 IE 中发生.火狐没问题.这也只有当我把它放在服务器上时才会发生.当我在本地运行它时,在 IE 和 FF 中一切正常.但是当我把它放到服务器上时,FF 仍然可以正常工作,而 IE 则不行.

This is ONLY happening in IE. FireFox is fine. This also is only happening when I put it on the server. When I run it locally, everything works fine, in both IE and FF. But when I put it on the server, FF still works fine, but IE does not.

有没有人见过这个,或者知道这里发生了什么?非常感谢任何帮助!

Has anyone seen this before, or know what's going on here? Any help is greatly appreciated!

推荐答案

在 IE 中访问 A 元素的 href DOM 属性时,会返回绝对路径到网址.对于 IE7 及更低版本中的 getAttribute() 也是如此(因为 getAttribute 在 IE8 之前被破坏).

When accessing the href DOM property of an A element in IE, it will return the absolute path to the url. The same is true for getAttribute() in IE7 and lower (since getAttribute was broken until IE8).

http://msdn.microsoft.com/en-us/library/cc848861(VS.85).aspx:

Internet Explorer 8 或更高版本.在 IE8 模式下,HREF 的值取决于对属性的引用的上下文.当作为文档对象模型 (DOM) 属性读取时,HREF 返回与托管网页的域相关的 URL.HREF 在页面以较早的文档兼容模式显示时,或在使用较早版本的浏览器查看页面时作为内容属性读取时,返回页面作者指定的值.有关详细信息,请参阅 Internet Explorer 8 中的属性差异.

Internet Explorer 8 or later. In IE8 mode, the value of the HREF depends on the context of the reference to the attribute. When read as a Document Object Model (DOM) attribute, HREF returns a URL relative to the domain hosting the Web page. HREF returns the value specified by the page author when read as a content attribute when the page is displayed in an earlier document compatibility mode, or when the page is viewed with an earlier version of the browser. For more information, see Attribute Differences in Internet Explorer 8.

如果命名约定相同,jQuery 将始终获取 DOM 属性:

jQuery will always fetch the DOM property if the naming convention is the same:

// If applicable, access the attribute via the DOM 0 way
if ( name in elem && notxml && !special ) {
    // ...
}

这里的 name in elem 部分是检查是否已指定 DOM 属性.要为 IE8 解决此问题,您可以指定大写属性 - .attr("HREF") - 因为 DOM 属性区分大小写.不幸的是,IE7 及更低版本的唯一解决方法是执行字符串替换:

The name in elem part here is checking to see if a DOM property has been specified. To work around this for IE8 you could specify the property in uppercase - .attr("HREF") - because DOM properties are case sensitive. Unfortunately the only workaround for IE7 and lower is to perform a string replace:

var base = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);
PageLink = PageLink.replace(base, ""); 

这篇关于JQuery 在 IE 中无法正确解析 attr("href")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆