【问题标题】:jQuery tab plugin not opening in a new browser tab when press 'ctrl' key按下“ctrl”键时,jQuery 选项卡插件未在新的浏览器选项卡中打开
【发布时间】:2014-05-07 23:30:15
【问题描述】:

我在我的页面中使用了 Jquery easy tab 插件。

当我右键单击每个选项卡并在新的浏览器选项卡中打开它时,它会显示出来,但是当我按下键盘上的ctrl 键并单击选项卡时,它将在同一个浏览器选项卡而不是新的浏览器选项卡中打开。

我该如何解决这个问题?

我的 jQuery 代码:

$(document).ready(function (e) {
    $("html,body").animate({
        scrollTop: 0
    }, 1000);
    var t = window.location.hash.substr(1);
    if (t == "oilngas") {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabOil").trigger("click");
        window.scrollTo(0, 0);
    } else if (t == "utilities") {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabUtl").trigger("click");
    } else if (t == "smart") {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabSmt").trigger("click");
        window.scrollTo(0, 0);
    } else if (t == "heavy") {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabHvy").trigger("click");
        window.scrollTo(0, 0);
    } else if (t == "") {
        $(".offerings_content").show();
    }

    $("#oil_area").click(function (e) {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabOil").trigger("click");

        //prijesh
        //var href = $('#tabOil').attr('href');
        //var href = window.location.hash.substr(1);

        //alert("href : " + href);
        //if (href == '#oilngas'){
        //  window.location.href = href; //causes the browser to refresh and load the requested url
        //}


        return false;
    });

    $("#utility_area").click(function (e) {


        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabUtl").trigger("click");
        return false;

    });
    $("#smartbldng_area").click(function (e) {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabSmt").trigger("click");
        return false;

    });
    $("#heavy_area").click(function (e) {
        $(".offerings_content").hide();
        $("#tab-container").show();
        $("#tabHvy").trigger("click");
        return false;

    });
});

Live page demo

【问题讨论】:

  • return false; on click handler 负责限制重定向到页面。

标签: javascript jquery tabs


【解决方案1】:

您需要通过维护一些标志来跟踪是否按下了CTRL 键(无论何时按下 CTRL 键,该标志都是真/假。

var cntrlIsPressed=false;
$(document).keydown(function(event){
    if(event.which=="17")
        cntrlIsPressed = true;
});

$(document).keyup(function(){
    cntrlIsPressed = false;
});

现在使用 cntrlIsPressed 标志,您可以确定您的返回值:

$("#oil_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabOil").trigger("click");

    if(!cntrlIsPressed)
        return false;
});

$("#utility_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabUtl").trigger("click");

    if(!cntrlIsPressed)
        return false;
});
$("#smartbldng_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabSmt").trigger("click");

    if(!cntrlIsPressed)
        return false;
});
$("#heavy_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabHvy").trigger("click");

    if(!cntrlIsPressed)
        return false;
});

编辑: 您还可以使用e.ctrlKey 来确定@​​987654329@ 密钥状态为Kamlesh Kushwaha 建议。

$("#oil_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabOil").trigger("click");

    if(!e.ctrlKey)
        return false;
});

$("#utility_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabUtl").trigger("click");

    if(!e.ctrlKey)
        return false;
});
$("#smartbldng_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabSmt").trigger("click");

    if(!e.ctrlKey)
        return false;
});
$("#heavy_area").click(function (e) {
    $(".offerings_content").hide();
    $("#tab-container").show();
    $("#tabHvy").trigger("click");

    if(!e.ctrlKey)
        return false;
});

注意:您还需要将链接指定为taget="_new"<a target="_new" ...> ... </a>

编辑: 看到您的页面后,我可以看到没有为您的标签链接定义 ID,this page。您的标签插件正在阻止重定向。

您可以在 jQuery 选择器中从他们的 href 属性中引用链接,因此您需要添加以下代码从 jQuery 打开新选项卡:

$("a[href='#company'], a[href='#founders'], a[href='#team'], a[href='#accolades'], a[href='#careers'], a[href='#philosophy']").click(function(e){
    //determine if control+click or mouse middle button
   if(e.ctrlKey==true || e.which==2){
        window.open($(this).attr("href"));
   }
});

我已经从您页面上的浏览器控制台测试了这段代码。它的工作:)

【讨论】:

  • 单击鼠标中心按钮时它工作正常,但仅在 Mozilla 中。当我按 ctrl 键时,它不会显示在新的浏览器选项卡中。
  • 这很令人困惑。你的意思是它不适用于中键点击?为此,您需要测试 e.which==2 值。
  • @Dhanil,我有你的问题。尝试在链接<a> 标签中指定target="_new"
  • 我试过了,但还是不行。当我按下 ctrl 键并单击选项卡时,它会在同一个浏览器选项卡中打开。检查我的页面flutura.com/flutura-trial/about_us_trial.php
  • 哇哦!!它的工作雅虎!谢谢兄弟/姐妹,非常感谢您的时间!!还有一个问题,问起来很尴尬,当我按下并单击任何非活动选项卡时,它会在新的浏览器选项卡中打开它很好,但它也会在同一页面中打开该选项卡任何解决方案?如果不是很好。
【解决方案2】:

使用 e.ctrlKey 。检查它的值,根据你可以使用return false 或简单的return 的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2018-08-29
    • 2014-03-19
    • 2016-05-04
    相关资源
    最近更新 更多