【Lynda高精度字幕由CGSUB.COM独家发布】 - You can get pretty far using the basic jQuery filters,
but you'll run into scenarios
where you need a little bit more power,
and jQuery provides some more advanced selectors
which we'll take a look at now.
So here in the example snippets,
let's scroll down to the Advanced Filters section
and open up the AttrFilters_start file.
Back in the snippets,
let's copy these first four lines over,
and I'll explain what each one does.
So, we'll paste that into the document,
and let's comment out these three for now.
The Attribute Filters in jQuery
let you perform various tests
to see if attributes are present
and optionally contain certain values.
So, in this first example,
we have a jQuery selector that's looking for paragraph tags,
and, for Attribute Filters,
we put the attributes inside these square brackets.
So in this case, we're looking for paragraph tags
that have the presence of a class attribute.
We don't care what the value is.
We're just testing to see if it's there,
and if we scroll down in the document,
you'll see that these paragraphs right here
don't have class attributes,
but these inside the example div container do,
and, in fact, there's two of them.
So, if this works correctly,
then we'll put a three pixel red border
around those paragraphs.
So let's try that.
We'll save, and we'll bring this up in the browser,
and we'll refresh.
You can see that paragraphs one and three,
the ones that have class attributes
are not surrounded by a red border.
All right, let's go back to the code.
Okay, let's comment that one out and try the next one.
Now, in this case, we're going to test
for a particular value in an attribute.
So, once again, we're looking for paragraph tags,
and here we have our square brackets,
and we're looking for an id attribute
that is equal to the string para1.
So, let's scroll down and see if there is one,
and sure enough, right here, the second paragraph
has an id that's equal to para1.
So we'll save, and we'll refresh,
and you can see that that paragraph
is now being selected and it has the border around it.
All right, back to the code.
Let's try the next one.
In this case, we're looking for paragraph tags again,
and, once again, we're looking for the id attribute,
but the little up arrow equals means starts with.
So we're checking to see if the id
starts with the string of para,
and if we look down here,
we can see that there are three of them.
This one has para1, para4, and para5.
So all of them start with the word para.
So, if everything's working,
then those three paragraphs should have red borders,
and we'll refresh,
and sure enough they do.
All right, so far so good.
One more time back to the code,
and let's uncomment this last example.
Now, in this last example,
we're going to look for more than one attribute.
So, once again, we're looking for paragraph tags,
but now we have two sets of square brackets.
We have this one, and we have this one.
In the first case, we're looking for ids
that start with the string para
like we had in our last example,
but now we're also looking for the presence
of a lang attribute,
and the star equals operator
means contains the text.
Not necessarily just starts with it,
just contains it.
So, we're looking for language attributes
that contain the text "en-".
All right, let's scroll down,
and you can see that these last two paragraphs
both have ids that start with para
and have language attributes that start with en-.
Okay, so let's try this.
We'll save,
and we'll refresh,
and sure enough, the last two paragraphs are being selected.
So, you can see that by using the Attribute Filters,
you can get some pretty fine grain filtering done
on your jQuery selectors when you're looking
for really specific pieces of content
that match really fine-tuned criteria.
【本字幕由CGSUB.COM独家制作发布】 【禁止用作一切商业用途】 【www.cgsub.com】