简体   繁体   English

需要Bootstrap Nav-Tabs显示和隐藏onClick?

[英]Need to have Bootstrap Nav-Tabs Show AND Hide onClick?

I am currently in need of my Bootstrap Nav-Tabs to do a few things. 我目前需要我的Bootstrap Nav-Tabs做一些事情。 I need them to: 我需要他们:

(once tab li has been clicked) (一旦点击标签li)

  • onClick: If tabs are closed open them. onClick:如果选项卡已关闭,请打开它们。 If they open, at the same scope/stack/bubble of code operation, I need a div directly above it to dissappear making the page smaller and moving the Nav-Tabs up underneath something else which looks graphically cool. 如果它们打开,在相同的范围/堆栈/气泡代码操作中,我需要在它上面的div直接消失,使页面变小并将Nav-Tabs移动到看起来图形酷的其他东西下面。 :) :)

  • onClick: If a tab is clicked on that is already opened the tab section closes and the div that closed above slides back down and fills the space. onClick:如果单击了一个已打开的选项卡,则选项卡部分将关闭,上面关闭的div将向下滑动并填充空格。 (no the accordian component doesnt work here its the wrong kind of click transition) (没有手风琴组件在这里不起作用的是错误的点击过渡)

  • onClick: If the tabs are open and the person clicks on a different tab than is open in the content div it simply switches to it like normal. onClick:如果选项卡是打开的,并且用户点击了不同于内容div中打开的选项卡,则只需像平常一样切换到它。

Therefore I have been trying to figure out the appropriate timing during the event of a click happening, in these new languages to me. 因此,我一直试图在这些新语言中找到发生点击的适当时机。 I am using JQuery because the transitions are what I was looking for. 我正在使用JQuery,因为转换是我正在寻找的。 I have a $('.link').on('click', function() {} ) and I have been trying a .bind('click', function() {} ) or even a .click() or on('click', function() {} ) I can't figure out which event I need to have happen. 我有$('.link').on('click', function() {} )我一直在尝试.bind('click', function() {} )甚至是.click()on('click', function() {} )我无法弄清楚我需要发生的事件。

Here is my code so far: 到目前为止,这是我的代码:

(Sorry about the spacings, it auto spaced very wierdly when I pasted it in :S) (抱歉这些间距,当我粘贴它时,它自动间隔很大:S)

    var isOpen = false;
    var tabOpenName = "";
    var liClicked = ""; 

    $(document).ready(function() {
        $(".link").on("click", function(event) {

        liClicked = $(event.target).valueOf("id"); //Grab link clicked

    // begin loop - Li
    $("#tabMenu li").each(function(index, li) {
        var currentListItem = $(li);

        if (currentListItem.attr("class") == "active"){
                isOpen = true;
                        tabOpenName = $(li).prop("id"); //Grab tab open

        } // end if

    }); // end selection loop


    if (isOpen == true) {

        alert(liClicked);       
        $("#tabContentId").slideToggle("slow");
        $("#learnMoreBox").slideToggle("slow");


    } // end if

    }) //end .on event
    }); // end query

I don't think that I need to provide the HTML here because this can be given back to me in Pseudo code with a naming convention using understandable simple terms like <div id="containerToHide"> etc... 我不认为我需要在这里提供HTML,因为这可以通过使用可理解的简单术语(如<div id="containerToHide">等)的命名约定以伪代码的形式返回给我。

As I mentioned above the #learnMoreBox is the placed above my Nav-Tabs that will disappear when the tabs are confirmed to be open. 正如我上面提到的,#edgeMoreBox位于我的Nav-Tabs上方,当确认标签打开时,它将消失。 The #tabContentId is the div where my class="data-content" lives. #tabContentId是我的class="data-content"所在的div。

Currently the site works to an extent. 目前该网站在一定程度上有效。 When the tabs are clicked on they work like tabs when clicking between Index# 0 - 4. However anytime the person clicks on a tab AND any of them are active, it toggles the slide. 当单击选项卡时,它们在索引#0 - 4之间单击时像标签一样工作。但是,无论何时用户单击选项卡并且其中任何一个处于活动状态,它都会切换幻灯片。 I only want this to happen when they click on the tab that is showing. 我只想在他们点击显示的标签时发生这种情况。 Not just any tab... 不只是任何标签......

I know I need to find the id of the <a href=""> that HAS BEEN CLICKED ON. 我知道我需要找到已经点击的<a href="">的ID。 lol. 大声笑。 I say that loudly because thats the 1 and only problem I am having so far. 我大声说,因为那是我迄今为止唯一的问题。 I have been trying to play with it and make my liClicked variable show me which one of my .link class <a href=""> list items was clicked and compare it to my tabOpenName variable to see if they clicked on the tab that was open. 我一直在尝试使用它并使我的liClicked变量显示我的liClicked<a href="">列出项目中的哪一个被点击并将其与我的tabOpenName变量进行比较以查看它们是否点击了标签打开。

I was hoping initially not to compare strings I was hoping to have the variables point to the selectedIndex number of the tab open and have conditionals that say if(selectedIndex == 0 && tabOpenName == 'Home') then {slide} ... etc. Im sure you can see where I am going with this. 我希望最初不要比较字符串我希望变量指向选项卡打开的selectedIndex数字,并且条件说明if(selectedIndex == 0 && tabOpenName == 'Home') then {slide} ...我相信你可以看到我要去哪里。

Now your probably wondering... gee... why does he have that alert in the last part? 现在你可能想知道......哎呀...为什么他在最后一部分有这种alert lol. 大声笑。 Its because I have found no IDE, Chrome Dev Tools or software to let me step through JQuery and thats how I traditionally learn languages. 这是因为我找不到IDE,Chrome开发工具或软件让我逐步完成JQuery,这就是我传统上学习语言的方式。 Therefore I was using the alert to see if I could somehow find a way to return a string inside the alert. 因此,我正在使用警报,看看我是否能以某种方式找到一种方法在警报内返回一个字符串。 If I could get a string to return in the alert I could easily compare it... The tabOpenName in the alert shows the exact string of the currently opened tab. 如果我可以在警报中返回一个字符串,我可以轻松地比较它...警报中的tabOpenName显示当前打开的选项卡的确切字符串。

Help would be greatly appreciated. 非常感谢帮助。

First of all, .on('click', function() {...}) is jQuery's preferred method for binding event handlers and .bind() is the method to use with older versions of jQuery. 首先, .on('click', function() {...})是jQuery用于绑定事件处理程序的首选方法,而.bind()是用于旧版本jQuery的方法。 Also, .click(function() {...}) is a shortcut for .on('click', function() {...}) but .click() without an event handler passed in is a trigger for the click event. 此外, .click(function() {...}).on('click', function() {...})的快捷方式,但没有传入事件处理程序的.click()是一个触发器点击事件。 Just in case you had any confusion on that front. 万一你在这方面有任何困惑。

Also, simply putting "debugger" in your code will cause Chrome Dev Tools to pause execution and open a debugger interface when you reach that line. 此外,只需在代码中加入“调试器”,Chrome Dev Tools即可暂停执行,并在到达该行时打开调试器界面。 For example: 例如:

if (isOpen == true) {

    debugger     
    $("#tabContentId").slideToggle("slow");
    $("#learnMoreBox").slideToggle("slow");


} // end if

Also refer to Debugging JavaScript at the Google Developers website for more info on using Chrome Dev Tools to debug JavaScript code. 另请参阅调试JavaScript的在谷歌开发者网站获取更多的信息上使用Chrome开发者工具调试JavaScript代码。

Finally, I'm not sure how much I can do without more context/code. 最后,我不确定没有更多的上下文/代码我能做多少。 Where do you add an "active" class to your list items? 在哪里为列表项添加“活动”类? Also it seems like you set isOpen to true and never set it to false again which will cause the " #tabContentId " and " #learnMoreBox " to slide every time a list item is clicked regardless of the situation. 此外,您似乎将isOpen设置为true并且再也不将其设置为false,这将导致每次单击列表项时“ #tabContentId ”和“ #learnMoreBox ”都会滑动,而不管情况如何。

//Edit 1: Here's what I would try, it might not work with exactly what you're set up but hopefully I can get the gist of my thinking across. //编辑1:这就是我要尝试的内容,它可能不适用于您所设置的内容但希望我可以得到我的想法的主旨。

var isOpen = false;
var tabOpenName = "";
var liClicked = ""; 

$(document).ready(function() {
    $(".link").click(function() {

        liClicked = $(this).attr("id"); //Grab id of link clicked

        if (isOpen) { //If there is an open link somewhere
            if ($(this).hasClass("active")) { //If the link just clicked is open
                $(this.).removeClass("active");
                isOpen = false; //Now there shouldn't be any link open
                $("#tabContentId").slideToggle("slow");
                $("#learnMoreBox").slideToggle("slow");
            } else { //If another link somewhere is open

                //Find and close the currently active link
                $(this).parent().find("li.active").removeClass("active");
                $(this).addClass("active"); //Mark this link as active

                //Do the content containers slide here? I'm still not clear on that haha

            } //End if-else cases where there is anything open at all
        } else { //Case where nothing is currently open
            $(this).addClass("active"); //Mark this link as open

            //Toggle the content area sliding animation
            $("#tabContentId").slideToggle("slow");
            $("#learnMoreBox").slideToggle("slow");

            isOpen = true; //We've opened a link now
        } //End If-Else

    }) //end click event handler
}); // end query

I'm still sort of guessing at the setup you have and what you want to happen, but an event handler set up like that hopefully can achieve what you want? 我仍然在猜测你所拥有的设置以及你想要发生的事情,但是一个像这样的事件处理程序设置可以达到你想要的效果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM