简体   繁体   English

主DIV上的JavaScript和jQuery切换不起作用

[英]JavaScript and jQuery toggle on main DIV not working

So, I'm making it where their is some buttons, and when one is clicked it makes the hidden DIV with contents unhide, this is what I have: 因此,我将其放置在其中有一些按钮的位置,当单击一个按钮时,将使隐藏内容的隐藏DIV取消隐藏,这就是我所拥有的:

HTML

<div id="disArea">   <!--The toggled Dac diplays in this area.-->
            <div class="Dac  1">
                <p>Dac 1/p>
            </div>

            <div class="Dac  2">
                <p>Dac 2/p>
            </div>

            <div class="Dac  3">
                <p>Dac 3/p>
            </div>

            <div class="Dac  4">
                <p>Dac 4/p>
            </div>

            <div class="Dac  5">
                <p>Dac 5/p>
            </div>
    </div>

            <div class="ibox" onclick="disToggle('.1')">Button 1 (Displays the Dac 1)</div>
            <div class="ibox" onclick="disToggle('.2')">Button 2 (Displays the Dac 2)</div>
            <div class="ibox" onclick="disToggle('.3')">Button 3 (Displays the Dac 3)</div>
            <div class="ibox" onclick="disToggle('.4')">Button 4 (Displays the Dac 4)</div>
            <div class="ibox" onclick="disToggle('.5')">Button 5 (Displays the Dac 5)</div>

Please remember that the div's with the class of "Dac" are hidden until jQuery executes the following (well, that is what I want to do, but it doesn't): 请记住,在jQuery执行以下操作之前,“ Dac”类的div是隐藏的(嗯,那是我想做的,但不是):

jQuery / JavaScript

function disToggle(DacNumClass)
{
  $("ibox").click(function()
  {
    $(DacNumClass).toggle();
  });
}

I've done this by using a JavaScript function with a argument, the argument calls out the Id, or class that I need to .toggle() . 我通过使用带有参数的JavaScript函数来完成此操作,该参数调用Id或我需要.toggle() This could be the complete wrong idea, correct me if I am wrong. 这可能是完全错误的主意,如果我错了,请纠正我。 How this works is ever Dac has a number class along with it, and the function argument is the class eg divToggle() Dac带有数字类,而函数参数是该类,例如divToggle()

And I have lastly my CSS, not sure if this important at all, but here it is: 最后,我有我的CSS,不确定这是否很重要,但是这里是:

CSS

#disArea {
width: 300px;
height: 300px;
margin-right: auto;
margin-left: auto;
background: green;
}

        .Dac {
        display: none;
        }

.ibox {
background: red;
display: block;
width: 125px;
height: 125px;
margin: 5px;
}

It doesn't seem to be toggling? 似乎没有切换? what have I done wrong? 我做错了什么?

jsFiddle Example jsFiddle示例

EDIT

*Fixed link with the /p> instead of </p> *使用/p>而不是</p>固定链接

Also, is it possible to have only one Dac Div open at a time? 另外,一次只能打开一个Dac Div吗?

Since disToggle itself is a click handler you should just toggle the target element inside the method. 由于disToggle本身是单击处理程序,因此您应该只在方法内部切换目标元素。

Also since the method disToggle is used as a inlined handler it should be added to the global scope, by default jsfiddle will add the given script in window.onload=function(){...} wrapper which will make the disToggle method a private one to the onload handler. 另外,由于方法disToggle用作内联处理程序,因此应将其添加到全局范围中,因此默认情况下,jsfiddle将在window.onload=function(){...}包装器中添加给定脚本,这会将disToggle方法disToggle私有一个到onload处理程序。 You need to select No Wrap - Body/Head in Left Side Panel -> Second Dropdown 您需要在Left Side Panel -> Second Dropdown选择No Wrap - Body/Head Left Side Panel -> Second Dropdown

function disToggle(DacNumClass) {
    $('.Dac').not(DacNumClass).hide()
    $(DacNumClass).toggle();
}

Demo: Fiddle 演示: 小提琴

A more jQuery-ish solution jQuery风格的解决方案

<div class="ibox" data-target=".1">Button 1 (Displays the Dac 1)</div>
<div class="ibox" data-target=".2">Button 2 (Displays the Dac 2)</div>
<div class="ibox" data-target=".3">Button 3 (Displays the Dac 3)</div>
<div class="ibox" data-target=".4">Button 4 (Displays the Dac 4)</div>
<div class="ibox" data-target=".5">Button 5 (Displays the Dac 5)</div>

then 然后

jQuery(function ($) {
    var $dacs = $('.Dac');
    $('.ibox').click(function () {
        var target = $(this).data('target');
        $dacs.not(target).hide();
        $(target).toggle();
    })
})

Demo: Fiddle 演示: 小提琴

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

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