how can i extract text after hash # in the href part from a tag?

前端 未结 8 1535
情话喂你
情话喂你 2021-02-07 02:22

I have following a tags:

Any tab1 Label
tab2 Label
Any tab3 Label<         


        
相关标签:
8条回答
  • 2021-02-07 02:41
    function tellMyName() {
        var str = this.href.split("#")[1];
        alert(str);
    }
    

    Not all the time only the hash part will be displayed on the link. Sometimes the Host is added to the href. By splitting the href with the hash (#) and get the second part of the split string, you'll get what you want.

    0 讨论(0)
  • 2021-02-07 02:43
    function tellMyName() {
      alert(this.hash.replace('#',''));
    }
    
    $('a').click(tellMyName);
    

    crazy fiddle

    0 讨论(0)
  • 2021-02-07 02:43
    function tellMyName() {
        var str = this.prop("hash").substr(1)
        alert(str);
    }
    
    this.prop("hash") will return the hash value i.e #tab1
    
    0 讨论(0)
  • 2021-02-07 02:56

    You can use something like this:

    ...
    var activeTab = $(this).attr("href");
    $(activeTab).fadeIn();
    ...
    

    This use href="#tab-id" for locate #tab-id element you want, and fade in.

    0 讨论(0)
  • 2021-02-07 03:01
    <script>
    function tellMyName()
    {
        var text = this.href;
        text = text.replace("#","");
        alert(text);
    }
    
    </script>
    
    0 讨论(0)
  • 2021-02-07 03:03

    You can do something like this

    var fragment = $('a#my-link')[0].hash.substr(1);
    

    Check it out.

    0 讨论(0)
提交回复
热议问题