如何使用jQuery更改特定的href属性?

时间:2016-09-01 14:59:27

标签: javascript jquery

我想用jQuery在我的页面中更改一个元素:

<a class="table-cell" title="Affordable Loan Solution™ Mortgage" href="/en/articles/affordable-loan-solution-mortgage.html" name="affordableloansolutionmortgage_image_topicgrid">

  <span class="linkwidth"> Affordable Loan Solution™ Mortgage </span>
  <img alt="Affordable Loan Solution™ Mortgage" src="/res/img/topic/article.gif">

</a>

我想更改自动生成的href并将其改为:

https://www.bankofamerica.com/mortgage/

在这种情况下我该怎么办?

1 个答案:

答案 0 :(得分:3)

attribute equals selector的帮助下使用name属性获取元素,并使用attr()方法更新href属性。

$('[name="affordableloansolutionmortgage_image_topicgrid"]').attr('href','https:‌​//www.bankofamerica.‌​com/mortgage/');
// or using class selector
$('.table-cell').attr('href','https:‌​//www.bankofamerica.‌​com/mortgage/');