问题
I have run into an issue where i want to filter with multiple data attributes using jquery find.
I have the following dom elements:
<span data-selected="no" data-position="y2016m2" data-type="project" data-workplaceid="113" data-languageid="5" data-industryid="3" data-id="pr15" class="month-item dark-text timeline-item project-item"></span>
<span data-selected="no" data-position="y2016m5" data-type="project" data-workplaceid="113" data-languageid="1" data-industryid="5" data-id="pr22" class="month-item dark-text timeline-item project-item"></span>
<span data-selected="no" data-position="y2016m6" data-type="project" data-workplaceid="113" data-languageid="3" data-industryid="3" data-id="pr13" class="month-item dark-text timeline-item project-item"></span>
<span data-selected="no" data-position="y2017m2" data-type="project" data-workplaceid="113" data-languageid="1" data-industryid="1" data-id="pr1" class="month-item dark-text timeline-item project-item"></span>
<span data-selected="no" data-position="y2018m4" data-type="project" data-workplaceid="113" data-languageid="5" data-industryid="2" data-id="pr75" class="month-item dark-text timeline-item project-item"></span>
I am trying to use the following negating statement to get rid of all the items that are not needed:
$('.timeline').find('.project-item[data-languageid!=5][data-industryid!=3]');
The problem with this statement and its behaving as an OR statement and not as an AND statement. So when i run this line of code i get the first element from the list and the third but the third elements data-industryid=3 so it shouldnt be there.
My question is that the double data attribute statement filtering works as an OR statement or as an AND cause how i see here it is working as an OR statement cause on of the id's is the same.
Any advice would be much appreciated. Thanks Trix.
回答1:
Use .not()
or :not()
$('.timeline').find('.project-item:not([data-languageid=5][data-industryid=3])');
Or .not
$('.timeline').find('.project-item').not('[data-languageid=5][data-industryid=3]');
you can also use :not():not()
$('.timeline').find('.project-item:not([data-languageid=5]):not([data-industryid=3])');
来源:https://stackoverflow.com/questions/49803344/multiple-data-attribute-filtering-with-jquery