[jQuery] jQuery is() with more than 1 element
For part of a javascript I'm working on I need to process elements in
a HTML document but only if they are of a specific type or are not
contained within a specific type. I wanted to use code as follows
$(html *).each
{
if $(this).is ('a, h1, h2, h3') return
// Processing goes here
}
I discovered that this could lead to problems if, for example there is
a <span> tag contained within an <a> tag (I know that isn't strictly
speaking legal HTML but I don't have any control over it. It's
generated server side and I don't work with the scripts responsible
for that). I ammeded my code thus:
$(html *).each
{
if $(this).is ('a, h1, h2, h3') return;
if $(this).parent ().is ('a, h1, h2, h3') return;
// Processing goes here
}
I did experiment with $(this).parents () as well but discovered that
it was extremely slow. Checking the immediate parent seems to be
sufficiant in my test cases.
However, I discovered that the ('a, h1, h2, h3') syntax didn't seem to
be working properly, it only seemed to be picking up my a tags and not
h1, h2, h3 or any other tags I tried checking for. I eventually
figured out that it only seems to be checking for the first element
type on the list. This forced the following rewrite:
$(html *).each
{
ignoreTheseTags = new Array ('a', 'h1', 'h2', 'h3');
for (thisTag = 0; thisTag < ignoreTheseTags.length; thisTag++)
{
if $(this).is (ignoreTheseTags [thisTag]) return;
if $(this).parent ().is (ignoreTheseTags [thisTag]) return;
}
// Processing goes here
}
which produces the results I want, but is EXTREMELY slow!
Is there some way to do this faster? Can I pass a list directly to
the is() function and if so, how? Or should I try a different
approach entirely?