【问题标题】:how to make the div autohide when click other button单击其他按钮时如何使div自动隐藏
【发布时间】:2013-11-22 08:45:26
【问题描述】:

我正在使用带有 div..evrything 的显示/隐藏滑块与我的编码一起工作。 但我希望我的 div 或我的内容会在我单击另一个按钮时自动隐藏(我的编码中的图像)..

我希望任何人都可以帮助我解决这个问题..提前谢谢..

这是我的html

<div id="slidingDiv">
    <a href="#" class="show_hide" rel="#slidingDiv11">
        <img src="../img/choose/mens.png">
    </a>

    <a href="#" class="show_hide" rel="#slidingDiv12">
        <img src="../img/choose/womens.png">
    </a>
    <div id="slidingDiv11">
        <img src="../img/choose/clothings.png"><br><br>
        <img src="../img/choose/shoes.png"><br><br>
        <img src="../img/choose/bags.png"><br><br>
        <img src="../img/choose/accessories.png">
    </div>
    <div id="slidingDiv12">
        <img src="../img/choose/clothings.png"><br><br>
        <img src="../img/choose/shoes.png"><br><br>
        <img src="../img/choose/bags.png"><br><br>
        <img src="../img/choose/accessories.png">
    </div>
</div>
<div id="slidingDiv1">
    <a href="#" class="show_hide" rel="#slidingDiv16">
        <img src="../img/choose/book.png">
    <a>
    <a href="#" class="show_hide" rel="#slidingDiv17">
        <img src="../img/choose/textbooks.png">
    </a>

    <div id="slidingDiv16">
        <img src="../img/choose/books1.png">
        <img src="../img/choose/books1.png">
        <img src="../img/choose/books1.png">
    </div>
    <div id="slidingDiv17">
        <img src="../img/choose/textbooks1.png">
        <img src="../img/choose/textbooks1.png">
        <img src="../img/choose/textbooks1.png">
    </div>
</div>

这是我的css

#slidingDiv, #slidingDiv1, #slidingDiv2, #slidingDiv3, #slidingDiv4, #slidingDiv5, #slidingDiv6, #slidingDiv7, #slidingDiv8, #slidingDiv9, #slidingDiv10, #slidingDiv11, #slidingDiv12, #slidingDiv13, #slidingDiv14, #slidingDiv15, #slidingDiv16, #slidingDiv17 {
    height:300px;
    padding:20px;
    margin-top:10px;
    border-bottom:5px;
    display:none;
}

这是我的 js

$(document).ready(function () {
    $('.show_hide').showHide({
        speed: 1000, // speed you want the toggle to happen 
        easing: '', // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
        changeText: 0, // if you dont want the button text to change, set this to 0
        showText: 'View', // the button text to show when a div is closed
        hideText: 'Close' // the button text to show when a div is open
    });
});

(function ($) {
    $.fn.showHide = function (options) {

        //default vars for the plugin
        var defaults = {
            speed: 1000,
            easing: '',
            changeText: 0,
            showText: 'Show',
            hideText: 'Hide'

        };
        var options = $.extend(defaults, options);

        $(this).click(function () {

            $('.toggleDiv').slideUp(options.speed, options.easing);
            // this var stores which button you've clicked
            var toggleClick = $(this);
            // this reads the rel attribute of the button to determine which div id to toggle
            var toggleDiv = $(this).attr('rel');
            // here we toggle show/hide the correct div at the right speed and using which easing effect
            $(toggleDiv).slideToggle(options.speed, options.easing, function () {
                // this only fires once the animation is completed
                if (options.changeText == 1) {
                    $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
                }
            });

            return false;
        });
    };
})(jQuery);

【问题讨论】:

  • 请注意正确格式化您的代码。它使您和其他人更容易阅读。
  • 你能做个小提琴吗,会有帮助的。
  • jsfiddle.net 摆弄它
  • 抱歉我的代码有点乱
  • @Tushar Gupta 已编辑

标签: javascript jquery html css cakephp-2.0


【解决方案1】:

使用 jQuery,

$("your_other_button").on('click', function() {
      $("your_div_to_hide").hide();
});

【讨论】:

  • 我在哪里可以写这个?
【解决方案2】:

使用 Jquery

$("other_button").on('click', function() {
    $("div_to_hide").toggle();
});

【讨论】:

  • 你可以在html正文以脚本标签结束之前添加这个
【解决方案3】:

由于您想在单击按钮时隐藏div,因此您应该在单击按钮时将其设为document 以隐藏。试试这样的。

$(".hide_show").click(function(event) { event.stopPropagation(); $(this).fadeToggle(1000);}); $(document).click(function() { $(".hide_show).fadeOut(1000); });

使用event.stopPropagation(); 停止文档事件以触发.hide_show 类。可能会对你有所帮助。

【讨论】:

  • 谢谢..对不起,我是编程新手..我应该放在哪里??以及如何使用 event.stopPropagation();
  • 对不起。好的,如果您有两个 divs 类 .click_toggle 和 toggle_div,并且您想在单击 click_toggle 时隐藏和显示 .toggle_div。你应该应用一个jquery 像这样$(".click_toggle").click(function() { $(".toggle_div").toggle(); }); 现在你想隐藏toggle_dive 当你点击它之外'你应该应用另一个像这样的代码$(document).click(function() { $(".toggle_div").hide(); }); 如果你不希望文档甚至触发.click_toggle 事件,你应该让它 `$(".click_toggle").click(function(e) ( e.stopPropagation();
  • 而不是$(".click_toggle").click(function() { $(".toggle_div").toggle(); });,因为如果不添加e.stopPropagation();可能会导致Js出现故障。记笔记。 event 与 e 相同。
  • 感谢您的帮助..它可以工作..但是当我点击它外部时,我的 div 没有隐藏,但它被禁用或消失了..我只想让我的 div 在我点击其他时向上滑动按钮..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
  • 2013-01-16
相关资源
最近更新 更多