JQuery的不正确解析ATTR

JQuery的不正确解析ATTR

本文介绍了JQuery的不正确解析ATTR(" HREF")在IE浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我希望有人能够阐明一些轻真是奇怪的问题。我使用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!

下面是我的code:

$.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浏览器。 Firefox是罚款。这还仅仅是发生时,我把它放在服务器上。当我在本地运行,一切正常,在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.

有没有人见过这个,或者知道这是怎么回事呢?任何帮助是极大AP preciated!

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

推荐答案

访问当的href DOM的财产 A 在IE中的元素,将返回的绝对路径的URL。为也是一样的getAttribute()在IE7和较低(因为的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 /库/ cc848861(VS.85)的.aspx

Internet Explorer 8或更高版本。在IE8模式中,在 HREF值依赖于参考属性的范围内。当读为文档对象模型(DOM)属性, HREF 返回URL相对于托管Web页面的域。 HREF 返回页面作者指定的值时,当页面显示在前面的文档兼容性模式,或者页面时查看与浏览器的早期版本读为内容属性。欲了解更多信息,请参阅属性的差异在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 ) {
    // ...
}

在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的不正确解析ATTR(" HREF")在IE浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:07