Here is a Fiddle
I want to get the href
attribute, I use $(this).attr('href')
but it does not work!
HTML :
<div class="wrap_atletas_interno">
<ul>
<li class="atleta">
<a href="teste.html">
<div class="nome_86_atleta">Antônio</div>
<img src="atletas/antonio_86px.jpg" />
</a>
</li>
<li class="atleta">
<div class="nome_86_atleta">Cauê</div>
<img src="atletas/caue_86px.jpg" />
</li>
<li class="atleta">
<div class="nome_86_atleta">Dudu</div>
<img src="atletas/dudu_86px.jpg" />
</li>
</ul>
</div>
JavaScript :
$('.atleta').click(function (e) {
e.preventDefault();
$('.atleta').removeClass('atleta_atual');
$(this).addClass('atleta_atual');
var h = $(this).attr('href');
alert(h);
$.get(h, function (data) {
//$(".detalhes_atleta").html(data).fadeIn("slow");
alert(h);
});
});
Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!
Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.
The HTML <a> href Attribute is used to specify the URL of the page that the link goes to. When the href attribute is not present in the <a> an element that it will not be a hyperlink. This attribute is used to specify a link to any address.
Check the updated fiddle
Changed
var h = $("a",this).attr('href');
$(this)
is referring to the parent li
of the link. You need to use
$(this).find('a').attr('href');
Also please fix your html
, block elements should not be inside inline elements.
In your case the div
should not be in the a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With