如何动态更改HTML链接的href属性 [英] How to change HTML link's href property dynamically

查看:409
本文介绍了如何动态更改HTML链接的href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC3视图上有一个HTML链接.

Hi I have a HTML link on my MVC3 View.

我想在用户每次单击时更改其href属性.

I want to change its href property each time user clicks it.

<a class="tabs" href="#educationDetails">
<input id="SubmitBtn" type="submit" value="Next" />
</a>

有什么办法可以解决这个问题.

Is there any way to solve this issue.

非常感谢

推荐答案

$(".tabs").click(function() {
   $(this).attr("href","newhref.com");
});

更新

您可以获得这样的属性值,

you can get attribute value like this,

$(this).attr("href")  //will return '#educationDetails'

所以您可以像这样检查该值,

so you can check that value like this,

$(".tabs").click(function() {
  if ($(this).attr("href") == "#tab1")
      $(this).attr("href","#tab2");
  else if ($(this).attr("href") == "#tab2")
      $(this).attr("href","#tab1");
});

UPDATE-2

如果您只想将#tab1更改为#tab2,则不要撤消.您也可以这样操作,

If you just want to change #tab1 to #tab2, not reverse. you can also do it like this way,

$('a.tabs[href="#tab1"]')​.click(function() {
    $(this).attr("href","#tab2");​
})​;​

这篇关于如何动态更改HTML链接的href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆