在IE7/8中window.location无法正常工作 [英] window.location not working correctly in IE7/8

查看:75
本文介绍了在IE7/8中window.location无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,里面有一个a.我想单击div并遵循与a相同的URL.这是HTML:

I have a div which has an a inside it. I want to click on the div and follow the same url as the a. This is the HTML:

<div class="overview white getchildurl">
    <p class="w70">
    05-02-2011
    </p>
    <a class="button" href="details/">Details</a>
    <span class="clear"></span>
</div>

我有这个脚本:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        window.location = href;
    });
});

这在Firefox 3.6,Chrome,Safari中有效,但在IE7和IE8中无效.它会重新加载页面,但停留在同一页面上.我检查了var href,它具有正确的url,但没有转到那里.我在做什么错了?

This works in Firefox 3.6, Chrome, Safari, but not in IE7 and IE8. It reloads the page, but stays on the same page. I checked the var href, it has the right url, but doesn't go there. What am I doing wrong?

谢谢.

将其设为绝对URL可使其正常工作.这是新的脚本:

Making it an absolute URL made it work. This is the new script:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        var host = window.location.hostname;
        window.location = 'http://' + host + '/' + href;
    });
});

推荐答案

我将其发布为其他答案,因为它是不同的答案.如何解决JavaScript问题,而不是解决HTML和CSS问题.

I am posting this as a different answer because it is a different answer. How to work through problem with JavaScript instead of work through it with HTML and CSS.

尝试更改您的JavaScript以显示,并确保这些值与您认为的一样

Try changing your JavaScript to display and make sure the values are what you think they are

$(this).click(function() {
    var href = $(this).children('a').first().attr('href');
    alert(href)
    alert(href.length)
    window.location = href;
});

这将显示hrefhref长度.确保长度匹配.如果长度太大,则意味着存在隐藏字符.

That will display the href and the href length. Make sure the length matches up. If the length is too big then that means there are hidden characters.

如果您确定问题是由于语法引起的,则可以使用JavaScript进行许多URL修复/转换.例如,可以在出现斜杠时删除斜杠

If you determine that the problem is due to syntax, many URL fixes/conversions can be done with JavaScript. For example, removing a trailing slash when there is one can be done with

$(this).click(function() {
    var href = $(this).children('a').first().attr('href');
    window.location = href.replace(/\/$/, '');
});

此外,如果可以解决该问题,则可以使用JavaScript将URL转换为绝对URL.其他任何麻烦甚至不可见的字符都可以删除或替换.

Also, the URL could be converted to an absolute URL using JavaScript if that is what will fix the problem. And any other troublesome or even invisible characters can be either removed or replaced.

您可以在互联网上发布一个网页,我们可以访问该网页在我们自己的IE副本上查看该问题吗?您可以发布可复制到我们的计算机并在浏览器中查看的HTML文件吗?您对另一个问题的评论告诉我您有某种XHTML严格的DOCTYPE.如果我们中的一个人能够使问题最终发生,那么更有可能得到解决.

Can you post a web page on the internet that we can access to see the issue on our own copies of IE? Can you post an HTML file that we can copy to our machine and view in a browser? Your comment on the other question tells me you have some sort of XHTML strict DOCTYPE. If one of us can make the issue happen on our end, it is more likely to be fixed.

这篇关于在IE7/8中window.location无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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