Jump to content
carcleo

Encontrar o URL e alterar o style do href

Recommended Posts

carcleo

Boas,

Tenho o seguinte php:

$paginaCorrente = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

e o seguinte HTML

<ul class="menu">
    <li><a href="?l1">Link 1</a></li>
    <li><a href="?l2">Link 2</a></li>
    <li><a href="?l3">Link 3</a></li>
    <li><a href="?l4">Link 4</a></li>
  </ul>

E o seguinte JS

    $(document).find( "ul.menu a[href='<?php echo "?".$paginaCorrente ?>']" ).addClass( 'linkVisitado' );

Isso permite eu encontrar os parâmetros da url e saber qual item de menu vou marcar.

Atualmente estou fazendo com JQuery.

Gostaria de saber se há como fazer uma condicional em CSS de forma que consiga varrer o url e comparar com o href do elemento a. Encontrando, altera o seu estilo. Na verdade adiciona Class='linkVisitado' a esse href.

Tentei das formas abaixo mas não funcionou.

ul.menu li a {    

  @if (ul.menu li a:href='?l1') background-color: rgb(0,0,0);
  @if ul.menu li a[href='?l1'] { background-color: rgb(0,0,0)};

}

 

Edited by carcleo

Share this post


Link to post
Share on other sites
JakeBass

Existe o pseudo-seletor :first-child

ul.menu li:first-child a {

	background-color: rgb(0,0,0);

}

 

Share this post


Link to post
Share on other sites
N3lson

Usa o data() do jquery

no link adiciona o data-xx(valorpretendido)

e no jquery o $.selector.data("xx")

ex:

<li><a href="?l1" data-xx="l1">Link 1</a></li>

var xx = $(".menu > li").data("xx");

if(xx == ??){ .... }

https://api.jquery.com/data/

Edited by N3lson

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.