Tí Pro
Tí Pro

Reputation: 281

What does "a[href*=#]:not([href=#])" code mean?

I don't understand clearly what does this code mean ?

a[href*=#]:not([href=#])

Thank you !

Upvotes: 28

Views: 41696

Answers (6)

Frank Petersen
Frank Petersen

Reputation: 49

This post is ancient, but incase anyone is still looking. Does this only grab:

<a href="#step1">yes</a>
<a href="page.php#step2">yes</a>

or will it also grab:

<a href="/path/to/page.php#step2">yes</a>

In testing, it looks like the first group only, and I need it to grab the first and the second.

Upvotes: 0

Luca Rainone
Luca Rainone

Reputation: 16468

Simply:

a[href*=#] 

gets all anchors (a) that contain # in href.

But with:

:not([href=#])

excludes anchors with href exactly equal to #.

Example:

<a href="#step1">yes</a>
<a href="page.php#step2">yes</a>
<a href="#">no</a> 

the selector gets the first two anchors, but it excludes the last.

For more details you can consult the attribute selectors chapter

Upvotes: 49

Baldr&#225;ni
Baldr&#225;ni

Reputation: 5640

Just in case anyone had the same problem as me with it and new version of jQuery : The solution is not to use a[href*=#]:not([href=#]) but

use

a[href*="#"]:not([href="#"])

This was a breaking change in jQuery 2.2.4 onwards.

Upvotes: 43

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15767

means that all elements with href attribute conatining '#', except those whose href attribute equals to #

Upvotes: 2

moonwave99
moonwave99

Reputation: 22817

That's a CSS3 selector that gets all the a whose href attribute contains a #, but are not just made up of the single # char.

e.g.

Matched

<a href="#home">Home</a>
<a href="index.html#contact">Contact</a>

Not Matched

<a href="#">Top</a>

Upvotes: 1

BenM
BenM

Reputation: 53208

It is a CSS selector that matches any a element that has a href attribute containing the # character, but not anchor tags which have only #.

So for example, it'll match: <a href="#test">Test Anchor</a>, but not <a href="#">Blank</a>

Upvotes: 0

Related Questions