2012-08-14 83 views
1

任何线索如何通过class="CenteredHeader"来访问每个th以通过jQuery向a标记添加更多参数?更改th标记值

<tr class="CenteredHeader"> 
    <th scope="col"></th> 
    <th scope="col"> 
     <a href="/BoxOffice/Movies/807?sort=MovieName&amp;sortdir=ASC">Pelicula</a> 
    </th> 
    <th scope="col"> 
     <a href="/BoxOffice/Movies/807?sort=TechnologyName&amp;sortdir=DESC">Tecnologia</a> 
    </th> 
</tr> 
+2

这个问题也可以帮助你:http://stackoverflow.com/questions/11369182/how-替换元素-attr-href -with-each-strip-url – 2012-08-14 12:06:15

+0

+1好问题。如果我在回答中投票,这只是公平的假设这是一个很好的问题:) – Nope 2012-08-14 12:10:34

回答

8
$('.CenteredHeader th a').each(function() { 
    $(this).attr('href', function(i, oldhref) { 
     return oldhref + '&amp;p1=111' 
    }); 
})​; 

您可以通过上述方案,如果你想使用不同的参数,每个a标签。

也可以做@François Wahl comment说:

$('.CenteredHeader th a').attr('href', function(i, oldhref) { 
    return oldhref + '&amp;p1=111' 
})​; 
+0

jQuery的最小版本是否支持用函数设置attr值?它看起来很酷:) – 2012-08-14 12:02:53

+0

@LiviuT。任何版本 – thecodeparadox 2012-08-14 12:05:06

+0

+1为好的答案。 – 2012-08-14 12:05:31

2

试试这个:

$('.CenteredHeader th a').attr('href', "/BoxOffice/Movies/807?sort=MovieName&amp;sortdir=ASC&amp;p1=111") 
+2

+1单线解决方案。据我所知,OP要改变每一个标签的所有'href'属性。这似乎做到了。 – Nope 2012-08-14 12:02:26

+1

@FrançoisWahl谢谢你。 – undefined 2012-08-14 12:03:37

+0

我不确定这是否合适,因为示例中的th具有两个具有不同href值的锚点。 – nnnnnn 2012-08-14 12:06:00

1
$('.CenteredHeader a').each(function(index, a) { 
    var newHref = $(a).attr('href')+'&key=value'; 
    $(a).attr('href', newHref); 
});