简体   繁体   English

定位event.currentTarget的子级

[英]targeting child of event.currentTarget

I found a simple tab-menu jQuery plugin that needs some adapting for a project of mine. 我找到了一个简单的选项卡jQuery插件,需要为我的项目进行一些调整。 The tabs in question - being absolutely positioned - are taken out of the flow and as such don't contribute to the wrapping div's height, so the background behind them don't show. 有问题的选项卡-位置完全正确-已从流程中删除,因此不会影响包装div的高度,因此不会显示其背后的背景。 I am trying to force the height of the wrapping div (containing the background image) to match the height of the selected tab (+400px for nav and header) and to achieve that on the fly I am adapting the original jQuery file. 我试图强制包装div的高度(包含背景图像)以匹配所选标签的高度(对于导航和标题,为+ 400px),并且要实现这一目标,我正在对原始jQuery文件进行调整。 Here is the code (with the few extra lines (commented 'added!') of mine). 这是代码(带有我的几行额外代码(注释为“ add!”))。

var cbpHorizontalMenu = (function () {
    var $listItems = $('#cbp-hrmenu > ul > li'),
        $menuItems = $listItems.children('a'),
        $body = $('body'),
        current = -1;

    function init() {
        $menuItems.on('click', open);
        $listItems.on('click', function (event) {
            event.stopPropagation();
        });
    }

    function open(event) {
        if (current !== -1) {
            $listItems.eq(current).removeClass('cbp-hropen');
        }
        var $item = $(event.currentTarget).parent('li'),
            idx = $item.index();
        if (current === idx) {
            $item.removeClass('cbp-hropen');
            //added!
            current = -1;
        } else {
            $item.addClass('cbp-hropen');
            current = idx;
            $body.off('click').on('click', close);
            var content2Height = jQuery(".cbp-hrsub").height() + 400;
            jQuery('#content2').height(content2Height); //added
        }
        return false;
    }

    function close(event) {
        $listItems.eq(current).removeClass('cbp-hropen');
        //added!
        current = -1;
    }
    return {
        init: init
    };
})();

It does something, but not what I need. 它可以执行某些操作,但不是我需要的。 It gets the height of the first div.cbp-hrsub and applies it (+400px) to the div.content2. 它获取第一个 div.cbp-hrsub的高度,并将其(+ 400px)应用于div.content2。 What I need is to target the current tab (a child of event.currentTarget, me thinks?), to calculate its height and to apply it to the content2 div. 我需要的是定位当前选项卡(我认为是event.currentTarget的子级),计算其高度并将其应用于content2 div。

This is a simplified HTML if that helps: 如果有帮助,这是一个简化的HTML:

<div class="content2">
<nav id="cbp-hrmenu" class="cbp-hrmenu">
    <ul>
        <li>
            <a href="#">tab 1</a>
            <div class="cbp-hrsub">
                <div class="cbp-hrsub-inner"> 
                    I am 1st tab, 100px height.
                </div>
            </div>
        </li>
        <li>
            <a href="#">tab 2</a>
            <div class="cbp-hrsub">
                <div class="cbp-hrsub-inner">
                    I am 2nd tab, 200px height.

                </div>
            </div>
        </li>
        <li>
            <a href="#" class="white06">Nigel's CV</a>
            <div class="cbp-hrsub">
                <div class="cbp-hrsub-inner"> 
                    I am 3rd tab, 300px height.
                </div>
            </div>
        </li>

    </ul>
</nav>

Just to clarify, I want to keep the original plugin as it is, just to insert something instead of my 2 lines, towards the end of file. 为了澄清起见,我想保持原始插件不变,只是在文件末尾插入一些内容而不是我的两行内容。 (var content2Height = jQuery(".cbp-hrsub").height() + 400;jQuery('#content2').height(content2Height); //added Thanks everyone for their time. (var content2Height = jQuery(“。cbp-hrsub”)。height()+ 400; jQuery('#content2')。height(content2Height); //添加了感谢大家的时间。

Zel ZEL

I get inconsistent results when targeting a parent-container with .parent(). 使用.parent()定位父容器时,得到不一致的结果。 In this line: 在这一行:

var $item = $(event.currentTarget).parent('li'),
        idx = $item.index();

Try instead to use .closest(): 尝试改用.closest():

var $item = $(event.currentTarget).closest('li'),
        idx = $item.index();

Oh, wait! 等一下! I see the issue: 我看到了这个问题:

var content2Height = jQuery(".cbp-hrsub").height() + 400;

You are retrieving all the .cbp-hrsub classed elements, here. 您将在此处检索所有.cbp-hrsub分类的元素。 It's going to try to return a height, and I'm not exactly certain how jQuery determines that when it's looking at an array, but I'm guessing it just picks the first element out of the array. 它会尝试返回一个高度,但我不确定jQuery在查看数组时如何确定它的高度,但是我猜测它只是从数组中选择第一个元素。

What you're really needing at this point, then, is something like this: 那么,此时您真正需要的是这样的东西:

var content2Height = $item.first(".cbp-hrsub").height() + 400;

THAT should give you the height of .cbp-hrsub contained within the current item (found above), and not that of the first .cbp-hrsub in the array. 那应该给您当前项目(位于上面)中包含的.cbp-hrsub的高度,而不是数组中第一个.cbp-hrsub的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM