2012-07-06 13 views
7

im her yöntem ile href değiştirmeye çalışarak,Öğenin attr href'ini her biri nasıl değiştirilir? // şerit url

here is demo, inspect a, you'll see there is no change

html:

<a href="#/news">News</a> 
<a href="#/news/detail">Detail</a> 
<a href="#/sport">Sport</a> 
<a href="#/sport/football">Football</a>​​​​​​​​​​​​ 

jQuery:

$('a').each(function() { 
    $(this).attr('href').replace('#/',''); //tried to erase #/ from all hrefs 
});​ 
+0

böyle attr için koşul yapabilir deneyin; sadece href'i bir alıcı olarak attr ile bir değişken olarak alın, değiştirin ve sonra bir setter olarak attr ile tekrar dışarı pompalayın. (ya da elclanrs gibi, sanırım sadece setter hepsini bir arada yapabilirsin!). –

cevap

14

Eğer irade yayınlanmıştır kodu değeriolarak alın 0 sonra replace değerleri doğru ama hemen sonucunu atar. Değiştirilen değerde attr'a geçmeniz gerekir. Aşağıdaki

$('a').each(function() { 
    var value = $(this).attr('href'); 
    $(this).attr('href', value.replace('#/','')); 
});​ 
+0

bu eklenti için çalışmıyor /: ben tekrar deneyin –

+0

anladım, başka bir şey engelledi, teşekkürler dostum! –

6
var href = $(this).attr('href'); 
$(this).attr('href', href.replace('#/','')); 
+0

teşekkürler mate .. :) –

1

Ayrıca href değerini kontrol ve Sen zinciri yerini alamaz

<script type="text/javascript"> 
$('a').each(function() { 
    var value = $(this).attr('href'); 
    if(value=='http://google.com') 
    { 
     $(this).attr('href', 'http://youtube.com'); 
    } 
});​ 
</script> 
İlgili konular