javascript - 单个 DIV 内有 2 个 onClick 事件

标签 javascript jquery html toggle slidetoggle

我想要实现的目标非常简单。我有一个带有展开/折叠图标和问号图标的 div。当用户单击这些图标之一时,相应的隐藏 div 应向下滑动。

当用户单击展开/折叠图标而不是问号时,此功能有效。

HTML:

<div class="toggle_head Header">
    <label>
        <img class="expandCollpse" src="http://png-1.findicons.com/files/icons/2338/reflection/128/expand_alt.png" height="30px;" />
        <img class="hide expandCollpse" src="http://png-2.findicons.com/files/icons/2338/reflection/128/collapse_alt.png" height="30px;" />
    </label>
    <label>Expand</label>
    <label class="showHelp">
        <img src="http://www.katherineemmons.com/wp-content/uploads/2013/06/question_mark-icon.png" height="30px;" />
    </label>
</div>
<div class="toggle_body">
    <button type="button" class="btn" style="float:right;">Close</button>
    <label>**Info from expand/collapse icon click** - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco</label>
</div>
<div class="helpBody">
    <label>**Info from HELP icon click** - Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco</label>
</div>

jQuery:

$(".toggle_body").hide();
$(".collapse").hide();
$(".helpBody").hide();

$(".toggle_head").click(function () {
    var $this = $(this);
    $this.next(".toggle_body").slideToggle("slow", function () {
        $this.find('img.expandCollpse').toggle();
    });
});

$(".showHelp").click(function () {
    var $this = $(this);
    $this.find('div.helpBody').slideToggle("slow");
});

$(".btn").click(function () {
    var $this = $(this);
    $this.closest(".toggle_body").slideUp();
})

这是我的 fiddle :http://jsfiddle.net/oampz/x7k9B/4/

感谢任何帮助。

最佳答案

这是一个已修复问题的更新 fiddle :http://jsfiddle.net/x7k9B/5/

div 正在消耗对帮助图标的点击。

这是更新后的代码:

$(".toggle_body").hide();
$(".collapse").hide();
$(".helpBody").hide();

$(".toggle_head").click(function (event) {
    var $this = $(this);

    // is the click event's target the showHelp label? 
    if ($(event.target).closest('label').hasClass('showHelp')) {
        $(".helpBody").slideToggle("slow", function () {
            $this.find('img.expandCollpse').toggle();
        });
        return;
    }

    // click event occurred somewhere else on the header div- not on showHelp...
    $this.next(".toggle_body").slideToggle("slow", function () {
        $this.find('img.expandCollpse').toggle();
    });
});

$(".btn").click(function () {
    var $this = $(this);
    $this.closest(".toggle_body").slideUp();
})

关于javascript - 单个 DIV 内有 2 个 onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22632645/

相关文章:

javascript - myanim2 css 属性未在 ie9 中应用

javascript - JavaScript 中的垂直 slider

php - jQuery lightbox 从数据库通过 PHP/Ajax 强制第一次双击

javascript - 自动滑出导航加载?

HTML - 如何让我的文字出现在我的图标旁边?

javascript - 用 express + webpack-dev-middleware/webpack-hot-middleware 替换 webpack-dev-server

javascript - 如何在 Javascript 中定义自动重新启动的简单倒计时

javascript - 控制选择框的宽度

jQuery 验证规则 addMethod 中的信用卡到期日限制 future 20 年

html - 使用nodejs将html转换为pdf