【问题标题】:Use JQuery to select parent element of "this" (element clicked)使用 JQuery 选择“this”的父元素(点击元素)
【发布时间】:2013-09-18 04:26:41
【问题描述】:

我有一个 jQuery 脚本,它创建一个轮播以在用户单击时左右旋转图像。起初,我不打算在一个页面上放置多个轮播,但现在需求已经到来。

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

这是脚本

$(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() 有效,但是如何使用选择器和新的 carousel 变量检查轮播是否为 :animated?

$(':animated', carousel) ?

【问题讨论】:

    标签: jquery-selectors


    【解决方案1】:

    在事件处理程序内部,变量:

    $(this)

    将为您提供调用元素。从那里,要获取包含的 div,您可以使用 parent() 函数:

    $(this).parent()

    使用它来遍历 DOM。

    【讨论】:

      【解决方案2】:

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

      $('.right-button img').click(function()
          {
              carousel = $(this).parent();
      
              //bunch of code
          }
      

      【讨论】:

        【解决方案3】:

        由于您的操作标签嵌套在轮播中的第一级,您可以在每个函数中执行此操作以了解它是否属于:

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

        实际上会得到你现在可以使用的父对象。

        【讨论】:

          猜你喜欢
          • 2011-05-04
          • 1970-01-01
          • 1970-01-01
          • 2019-05-17
          • 1970-01-01
          • 2012-04-12
          • 1970-01-01
          • 2021-02-23
          • 1970-01-01
          相关资源
          最近更新 更多