go to link on button click - jquery

前端 未结 5 1731
情书的邮戳
情书的邮戳 2021-01-31 17:41

I have a script as below

$(\'.button1\').click(function() {
    document.location.href=$(this).attr(\'id\');
});

the button1 has variable uniqu

相关标签:
5条回答
  • 2021-01-31 17:53
    $('.button1').click(function() {
       window.location = "www.example.com/index.php?id=" + this.id;
    });
    

    First of all using window.location is better as according to specification document.location value was read-only and might cause you headaches in older/different browsers. Check notes @MDC DOM document.location page

    And for the second - using attr jQuery method to get id is a bad practice - you should use direct native DOM accessor this.id as the value assigned to this is normal DOM element.

    0 讨论(0)
  • 2021-01-31 17:59
    $('.button1').click(function() {
       document.location.href='/index.php?id=' + $(this).attr('id');
    });
    
    0 讨论(0)
  • 2021-01-31 18:07

    you can get the current url with window.location.href but I think you will need the jQuery query plugin to manipulate the query string: http://plugins.jquery.com/project/query-object

    0 讨论(0)
  • 2021-01-31 18:13

    You need to specify the domain:

     $('.button1').click(function() {
       window.location = 'www.example.com/index.php?id=' + this.id;
     });
    
    0 讨论(0)
  • 2021-01-31 18:14

    Why not just change the second line to

    document.location.href="www.example.com/index.php?id=" + $(this).attr('id');
    
    0 讨论(0)
提交回复
热议问题