关闭div点击<a> jquery

时间:2015-05-02 09:43:11

标签: javascript jquery html css

I use this code to display a div onclick:

;(function($) {
    $(function () {
        $('.m-help .m-text').hide();
        $('.m-help')
            .live('click', function () {
                $(this).find('.m-text').show();
            })
            .live('click', function () {
                $(this).find('.m-link-close-button').hide();
            });
    });
    $(document).bind('m-ajax-after', function (e, selectors) {
        $('.m-help .m-text').hide();
    });

})(jQuery);

And with this HTML:

<div class="m-help">
    <div class="m-text" style="width: 40px;">
        <?php echo $_helpHtml ?>
        <a href="#" class="m-link-close-button"><span>x</span></a>
    </div>
    <a href="#" onclick="return false;" class="details m-link"></a>
</div>

This does work onclick to display the div, but I want to use the X mark inside the div to close the div again. Closing the div does not work.

What am I doing wrong and how can I fix this problem?

4 个答案:

答案 0 :(得分:1)

活动授权

$('.m-help').on('click', function (event) {
    var close = $(event.target).closest('.m-link-close-button').length;
    $(this).find('.m-text')[close ? 'hide' : 'show']();
});

<强> http://jsfiddle.net/moogs/9e47j19L/1/

答案 1 :(得分:0)

在标记上使用click事件来关闭div:

function($) {
    $(function () {
        $('.m-help .m-text').hide();
        $('.m-help')
            .live('click', function () {
                $(this).find('.m-text').show();
            });

            $(".m-link-close-button").live('click', function () {
               $(this).closest('.m-text').hide();
            });
    });
    $(document).bind('m-ajax-after', function (e, selectors) {
        $('.m-help .m-text').hide();
    });

})(jQuery);

答案 2 :(得分:0)

您可以使用parent()获取关闭按钮的父级,以便您可以这样做:

(function($){
   $('.m-help .m-text').hide();
   $('.m-help').live('click', function(ev) {
       $(this).find('.m-text').show();
   });
   $(".m-link-close-button").live('click', function (ev) {
           $(this).parent().hide();
   });
})(jQuery)

答案 3 :(得分:0)

试试这个: HTML

$(".m-text").hide();
   $(".m-link").click(function () {
   $(".m-text").show();
});
   $(".m-link-close-button span").click(function () {
   $(".m-text").hide();
});

JS:

while

工作小提琴:http://jsfiddle.net/cncf5Lz9/