jquery基于类添加href

时间:2017-06-16 14:16:12

标签: javascript jquery

基于以下链接作为示例:

...如果链接标记包含类" add-selected",我希望jquery添加一个名为"#scrollTo"的href。对所有相应的。

<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>

页面加载后的最终结果应为:

<a class="add-selected btn btn-default" href="#scroll">
    <span class=" ion-plus-circled"></span>Add Selected
</a>

2 个答案:

答案 0 :(得分:2)

$('.add-selected').attr('href', '#scroll');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>
<br/>
<a class="add-selected btn btn-default" href="#">
    <span class=" ion-plus-circled"></span>Add Selected
</a>

答案 1 :(得分:1)

试试这个:

$(function(){
   $('a.add-selected').attr('href','#scrollTo');
});