简体   繁体   English

如何动态更改HTML链接的href属性

[英]How to change HTML link's href property dynamically

Hi I have a HTML link on my MVC3 View. 嗨,我在MVC3视图上有一个HTML链接。

I want to change its href property each time user clicks it. 我想在用户每次单击时更改其href属性。

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

Is there any way to solve this issue. 有没有办法解决这个问题。

Many Thanks 非常感谢

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

UPDATE UPDATE


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 UPDATE-2


If you just want to change #tab1 to #tab2, not reverse. 如果您只想将#tab1更改为#tab2,则不要反向。 you can also do it like this way, 你也可以这样

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

It is more efficient this way compared to @ocanal solution. 与@ocanal解决方案相比,这种方式更有效。

Source: 资源:

http://net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/ http://net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM