代码之家  ›  专栏  ›  技术社区  ›  BDuelz

使用JQuery选择“this”的父元素(单击元素)

  •  13
  • BDuelz  · 技术社区  · 16 年前

    我有一个jQuery脚本,可以创建一个旋转木马,在用户单击时左右旋转图像。起初,我不打算在一个页面上放置多个旋转木马,但现在需要了。

    问题是,当用户单击按钮时,我不知道如何引用一个carousel(单击的那个)。

    这是剧本

    $(function()
    {
        // Put last item before first item, in case user clicks left
        $('.carousel li:first').before($('.carousel li:last'));
    
        // Right click handler
        $('.right-button img').click(function()
        {
            // Get width plus margins
            var item_width = $('.carousel li').outerWidth() + (2 * parseInt($('.carousel li').css('margin-right')));
    
            // Get left indent
            var orig_left_indent = $('.carousel').css('left');
    
            // Calculate new left indent
            var left_indent = parseInt(orig_left_indent) - item_width;
    
            $('.carousel:not(:animated)').animate({'left': left_indent}, 500, 'swing', function()
            {
                // Put first item after last item
                $('.carousel li:last').after($('.carousel li:first'));
    
                // Set left indent to default
                $('.carousel').css({'left': orig_left_indent});
            });
        });
    
        // Left click handler
        $('.left-button img').click(function()
        {
            // Get width plus margins
            var item_width = $('.carousel li').outerWidth() + (2 * parseInt($('.carousel li').css('margin-right')));
    
            // Get left indent
            var orig_left_indent = $('.carousel').css('left');
    
            // Calculate new left indent
            var left_indent = parseInt(orig_left_indent) + item_width;
    
            $('.carousel:not(:animated)').animate({'left': left_indent}, 500, 'swing', function()
            {
                // Put last item before first item
                $('.carousel li:first').before($('.carousel li:last'));
    
                // Set left indent to default
                $('.carousel').css({'left': orig_left_indent});
            });
        });
    
        // Close button handler
        $('.carousel-container .close-button img').click(function()
        {
            $('.carousel-container').fadeOut(1000);
        });
    });
    

    现在,当您在任何转盘上单击“右”或“左”时,所有转盘都会移动。我不知道如何获得对旋转木马的引用。

    这是HTML

    <div class="carousel-container clearfix">
        <div class="header close-button">
            <strong>Check These Out</strong>
            <?php echo html::image('media/images/close.gif'); ?></div>
        <div class="left-button"><?php echo html::image('media/images/left_arrow.png'); ?></div>
            <div class="carousel-inner">
                <ul class="carousel">
                    <?php foreach ($items as $item): ?>
                    <li> ... </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <div class="right-button"><?php echo html::image('media/images/right_arrow.png'); ?></div>
    </div>
    

    如何实现这一点,我不知道如何引用用户单击的旋转木马,因为箭头是旋转木马的子元素。

    谢谢你的回答。 carousel = $(this).parent()

    $(':动画',旋转木马)?

    3 回复  |  直到 16 年前
        1
  •  35
  •   riwalk    16 年前

    在事件处理程序中,变量:

    $(this)

    会给你打电话的。从这里,要获取包含的div,可以使用parent()函数:

    $(this).parent()

    用它来遍历DOM。

        2
  •  2
  •   Peter Anselmo    16 年前

    使用.parent()函数向上一级。您的代码可能如下所示:

    $('.right-button img').click(function()
        {
            carousel = $(this).parent();
    
            //bunch of code
        }
    
        3
  •  2
  •   leeand00    12 年前

    由于您的操作标记嵌套在旋转木马的第一层中,因此您可以在每个函数中执行此操作,以确定它属于:

    var parent = $(this).parent().get(0);