How to change HTML link's href property dynamically

后端 未结 2 964
梦毁少年i
梦毁少年i 2021-01-22 13:59

Hi I have a HTML link on my MVC3 View.

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



        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 14:35

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

    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


    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");​
    })​;​
    

提交回复
热议问题