简体   繁体   English

选择没有特定类的jQuery First Child

[英]Selecting jQuery First Child without a specific class

I have a page with nested divs being used as Js/Jq tabs. 我有一个页面,其中嵌套的div被用作Js / Jq选项卡。 When you click on a parent tab, it fires the click event of the first child underneath the parent. 当您单击父选项卡时,它将触发父项下的第一个子项的click事件。

$('#topRow .pricing').click(function () {
    $('#secondRow .pricing').css('display', 'block');
    $('#secondRow .pricing div.tab:first-child').trigger('click');
});

This functionality works great as is. 此功能按原样工作良好。 However, we're adding functionality to effectively "lock" tabs by adding a "locked" class dynamically via c#/linq and then on window.ready we unbind all the tabs with the "locked" class and color them grey to indicate that they are disabled. 但是,我们通过c#/ linq动态添加“锁定”类,然后在window.ready上添加了功能,以有效地“锁定”选项卡。我们已经将所有选项卡与“锁定”类解除绑定,并将它们涂成灰色以指示它们被禁用。

I'm trying to modify the last line of the jQuery code above to click the first child that DOESN'T have the "locked" class. 我正在尝试修改上述jQuery代码的最后一行,以单击第一个没有“锁定”类的孩子。

    $('#secondRow .pricing div.tab:first-child').trigger('click');

Translating that into plainspeak, it's saying "In the Second Row, fire the click event of the first child tab in the 'pricing' group". 将其翻译成简而言之,就是说“在第二行中,触发“定价”组中第一个子选项卡的click事件”。 I'd like it to say "In the Second Row, fire the click event of the first child tab in the 'pricing' group that DOES NOT have the class 'locked'". 我想说“在第二行中,在“定价”组中触发第一个子选项卡的click事件,该类没有“锁定”的类。”

I've tried using: 我试过使用:

    $('#secondRow .pricing div.tab:not(.locked):first-child').trigger('click');

Which seems the correct way to do this, but it still looks at the first child and just doesn't fire the trigger('click'). 这似乎是执行此操作的正确方法,但它仍会查看第一个孩子,并且不会触发触发器(“单击”)。 Am I missing something? 我想念什么吗?

Here is the html: 这是html:

<div id="topRow">               
    <div class="pricing tab" runat="server" id="pricingTop">
        <div class="tabLeft">
            <div class="tabMain">
                Pricing
            </div>
        </div>
    </div>
</div>
<div id="secondRow">    
    <div id="pricingTabGroup" runat="server" class="pricing tabGroup" style="display:none">
        <div id="pricingProductA" runat="server" class="tab locked pricingProductA">
            <div class="tabLeft">
                <div class="tabMain">
                    ProductA
                </div>
            </div>
        </div>
        <div id="pricingProductB" runat="server" class="tab pricingProductB">
            <div class="tabLeft">
                <div class="tabMain">
                    ProductB
                </div>
            </div>
        </div>
    </div>
</div>

The "locked" class is added through C#: 通过C#添加“锁定”类:

protected void Page_Load(object sender, EventArgs e)
{
    bool ProductA = //some linq query

    if (!ProductA)
    {
        LockTab(pricingProductA);
    }

    protected void LockTab(HtmlGenericControl tab)
    {
        //Adds the "locked" class
        tab.Attributes.Add("class", "locked");
    }
}

Try 尝试

$('#secondRow .pricing div.tab').not('.locked').children().first().trigger('click');

Your selector looks fine though. 您的选择器看起来不错。 Usually a good idea to minimize your selector complexity. 通常,最好是将选择器的复杂性降至最低。

Also remember that every time you do a selector jQuery traverses the dom, so its best to save returned elements for a given selector as a variable(for performance) eg: 还要记住,每次执行选择器时,jQuery都会遍历dom,因此最好将给定选择器的返回元素保存为变量(以提高性能),例如:

var $tab_containers = $('#secondRow .pricing div.tab');

And then use it multiple times like 然后多次使用

$tab_containers.not('.locked').children().first().trigger('click');

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

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