简体   繁体   English

多个ID jQuery选择器返回的结果与单个ID选择器返回的结果不同

[英]Multiple ID jQuery Selector returns different than when a single ID selector

So i have some dropdown menus which I have originally setup to figure out which dropdown has been selected then loads the elements, no problems. 所以我有一些下拉菜单,这些菜单最初是由我设置的,以找出选择了哪个下拉菜单,然后加载元素,没有问题。

Now I've added a new set of dropdowns and my existing function now applies to this one as well because it was very broad 现在,我添加了一组新的下拉列表,并且我现有的功能现在也适用于此功能,因为它非常广泛

$(document).on('click', '.dropdown-menu li a', function () {...}

Now i have these dropdowns on 4 different tabs in a modal and I only want the last tab to function differently than the other three, so I want to make a function which works for the original 3 tabs and one for the 4th type of tab. 现在,我在模式中的4个不同选项卡上具有这些下拉菜单,并且我只希望最后一个选项卡的功能与其他三个选项卡不同,因此我想制作一个功能,该功能适用​​于原始的3个选项卡,而一个功能则适用于第4种类型的选项卡。

First three tabs IDS 前三个标签IDS

#racksTab
#condenserTab
#glycolTab

Fourth tab (different) #alertTab 第四个标签(不同)#alertTab

So my question is why do i get different results when my selector uses multiple IDs compared to just 1 所以我的问题是,为什么我的选择器使用多个ID而不是1时会得到不同的结果

$(document).on('click', '#racksTab,#condenserTab,#glycolTab .dropdown-menu li a', function () {...}

This function gives me 这个功能给我 多个ID选择器

But if i make the selector only use one id, it gets the child node i want 但是,如果我使选择器仅使用一个ID,它将获得我想要的子节点

$(document).on('click', '#racksTab .dropdown-menu li a', function () {...}

单ID选择器

Because with this selector : 因为有了这个选择器:

#racksTab,#condenserTab,#glycolTab .dropdown-menu li a #racksTab,#condenserTab,#glycolTab .dropdown-menu li a

You are targeting three elements: 您的目标是三个元素:

  1. Element with Id racksTab 带ID racksTab元素

  2. Element with Id condenserTab 带ID condenserTab元件

  3. a element inside li child of .dropdown-menu child of Id element glycolTab a内部元素li的孩子.dropdown-menu标识元素的子glycolTab

I guess you want all elements like the third one, so your selector must be: 我想您希望所有元素都像第三个一样,所以您的选择器必须是:

#racksTab .dropdown-menu li a, #condenserTab .dropdown-menu li a, #glycolTab .dropdown-menu li a #racksTab .dropdown菜单a,#condenserTab .dropdown菜单a,#glycolTab .dropdown菜单a



Note: Maybe you don't need those selectors at all, I think you can improve your code with a more suitable selector as @j08691 points on the comments, but all will be based on the real markup, if you add it we can help U improve the code. 注意:也许您根本不需要那些选择器,我认为您可以使用更合适的选择器来改进代码,因为@ j08691指向注释,但所有这些都将基于真实的标记,如果您添加它,我们可以为您提供帮助ü完善代码。

我认为这可能是您的目标:

$(document).on('click', '#racksTab .dropdown-menu li a, #condenserTab .dropdown-menu li a, #glycolTab .dropdown-menu li a'

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

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