代码与 IE 不兼容?

发布于 2024-08-03 18:09:28 字数 875 浏览 3 评论 0原文

$(document).ready(function() {
    var url = document.location.toString();
    $('.tab').click(function() {
        if($(this).is(".active")) {
            return;
        }

        var classy = $(this).attr("class").split(" ").splice(-1);
        var innerhtml = $('.content.'+classy).text();
        $('#holder').html(innerhtml);
        $('.tab').removeClass('active');
        $(this).addClass('active');
    });

    var url = document.location.toString();

    if(url.match(/#([a-z])/)) {
        //There is a hash, followed by letters in it, therefore the user is targetting a page.  
        var split = url.split("#").splice(-1);
        $('.tab.'+split).click();
    }
    else {
        $('.tab:first').click();
    }
});

嘿,我的一位评论者刚刚告诉我,这段代码在 IE 中不起作用。我一生都无法弄清楚为什么。每当您切换选项卡时,选项卡的内容都不会更改。同时#holder div 的内容是所有选项卡的组合。

有什么想法吗?

$(document).ready(function() {
    var url = document.location.toString();
    $('.tab').click(function() {
        if($(this).is(".active")) {
            return;
        }

        var classy = $(this).attr("class").split(" ").splice(-1);
        var innerhtml = $('.content.'+classy).text();
        $('#holder').html(innerhtml);
        $('.tab').removeClass('active');
        $(this).addClass('active');
    });

    var url = document.location.toString();

    if(url.match(/#([a-z])/)) {
        //There is a hash, followed by letters in it, therefore the user is targetting a page.  
        var split = url.split("#").splice(-1);
        $('.tab.'+split).click();
    }
    else {
        $('.tab:first').click();
    }
});

Hey, I was just informed by one of my commenters that this code doesn't work in IE. I can't for the life of me figure out why. Whenever you switch tabs, the content of the tab doesn't change. Meanwhile the content of the #holder div is all the tabs combined.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野心澎湃 2024-08-10 18:09:28

不是您想要的答案,但我会 严重建议如果可以的话,研究jQueryui选项卡小部件。至少,它让我的生活更容易处理这些事情。

Not the answer you're after, but I'd seriously recommend looking into the jQueryui tabs widget if you can. It's made my life a lot easier dealing with this stuff at least.

峩卟喜欢 2024-08-10 18:09:28

如果没有 IE 版本和页面来查看到底发生了什么,很难说清楚 - 但这里有一些最好的猜测:

更改:

if($(this).is(".active")) {

到:

if($(this).hasClass("active")) {

更改:

var innerhtml = $('.content.'+classy).text();

到:

var innerhtml = $('.content .'+classy).text(); // note the space

更改:

var url = document.location.toString();

到:

var url = document.location.hash;

Hard to tell without an IE version and a page to look at what exactly is happening- but here are some best guesses:

change:

if($(this).is(".active")) {

to:

if($(this).hasClass("active")) {

change:

var innerhtml = $('.content.'+classy).text();

to:

var innerhtml = $('.content .'+classy).text(); // note the space

change:

var url = document.location.toString();

to:

var url = document.location.hash;
紧拥背影 2024-08-10 18:09:28

我做了 Ryan 建议的所有更改,除了根据需要在“.content”和句点之间添加空格之外。如果没有源代码,他就不可能知道。

我将您的 .splice(-1) 更改为 [1],以便我选择数组中的第二项,即类名。看起来 .splice(-1) 在 IE 和其他浏览器中的行为有所不同。

我已经用 IE 7-8 测试了代码,它可以工作。

现在的源代码:

$(document).ready(function() {
    var url = document.location.hash;

    $('.tab').click(function() {
        if ($(this).hasClass("active")) {
            return;
        }

        var classy = $(this).attr("class").split(" ")[1];
        var innerhtml = $('.content.' + classy).text();

        $('#holder').html(innerhtml).slideDown("slow");

        $('.tab').removeClass('active');
        $(this).addClass('active');
    });

    if (url.match(/#([a-z])/)) {
        //There is a hash, followed by letters in it, therefore the user is targetting a page.  
        var split = url.split("#")[1];
        $('.tab.' + split).click();
    }
    else {
        $('.tab:first').click();
    }
});

I did all changes which Ryan suggested except adding the space between '.content' and the period as it is needed. He could not have known without the source code.

I changed your .splice(-1) to [1] so that I'm choosing the second item in the array, which is the class name. It looks like .splice(-1) is behaving differently in IE and other browsers.

I have tested the code with IE 7-8 and it works.

Source code as it is now:

$(document).ready(function() {
    var url = document.location.hash;

    $('.tab').click(function() {
        if ($(this).hasClass("active")) {
            return;
        }

        var classy = $(this).attr("class").split(" ")[1];
        var innerhtml = $('.content.' + classy).text();

        $('#holder').html(innerhtml).slideDown("slow");

        $('.tab').removeClass('active');
        $(this).addClass('active');
    });

    if (url.match(/#([a-z])/)) {
        //There is a hash, followed by letters in it, therefore the user is targetting a page.  
        var split = url.split("#")[1];
        $('.tab.' + split).click();
    }
    else {
        $('.tab:first').click();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文