javascript - jQuery: navigate to another page by clicking a button - Stack Overflow

I'm learning JavaScript and jQuery. I'm trying to build a simple web page with buttons that n

I'm learning JavaScript and jQuery. I'm trying to build a simple web page with buttons that navigate to another page upon click via a switch-case statement.

I've added a local jQuery script to my .html file, but nothing seems to happen when I click the buttons.

Demo can be found here: /

The html code is:

<head>
 <script src="//ajax.googleapis/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

 $("input[type='button']").click(function() {
     switch(this.id) {
         case 'x':
             window.location.href = ""; 
             break;
         case 'y': 
             window.location.href = ""; 
             break;
         case 'z': 
             window.location.href = ""; 
             break;
            }
        });    
</head>

<body>        
    <input type="button" value="google" style="width:120px" id="x"><br>
    <input type="button" value="yahoo" style="width:120px" id="y"><br>
    <input type="button" value="stackoverflow" style="width:120px" id="z"><br>
</body>

I'm learning JavaScript and jQuery. I'm trying to build a simple web page with buttons that navigate to another page upon click via a switch-case statement.

I've added a local jQuery script to my .html file, but nothing seems to happen when I click the buttons.

Demo can be found here: http://jsfiddle/VmYLy/

The html code is:

<head>
 <script src="//ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>

 $("input[type='button']").click(function() {
     switch(this.id) {
         case 'x':
             window.location.href = "http://google."; 
             break;
         case 'y': 
             window.location.href = "http://yahoo."; 
             break;
         case 'z': 
             window.location.href = "http://stackoverflow."; 
             break;
            }
        });    
</head>

<body>        
    <input type="button" value="google" style="width:120px" id="x"><br>
    <input type="button" value="yahoo" style="width:120px" id="y"><br>
    <input type="button" value="stackoverflow" style="width:120px" id="z"><br>
</body>
Share Improve this question edited Dec 28, 2015 at 15:05 Nat Naydenova 7992 gold badges12 silver badges30 bronze badges asked Mar 22, 2014 at 12:15 AlexAlex 2,0724 gold badges42 silver badges76 bronze badges 5
  • 1 have you actually tried this in your machine or just in jsfiddle? I'm afraid you can't redirect in jsfiddle – G.Mendes Commented Mar 22, 2014 at 12:18
  • it seems to be working fine, fiddle trows error Refused to display 'http://stackoverflow./' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. – T J Commented Mar 22, 2014 at 12:19
  • of course i've tried this on my machine. nothing happens :/ how can i be sure the jquery library is loaded correctly to the browser? – Alex Commented Mar 22, 2014 at 12:20
  • it doesn't work in jsfiddle but when you look in your console you find this: Load denied by X-Frame-Options: google.de/?gfe_rd=cr&ei=8X8tU5uSGMmK8Qeg7YCgDg does not permit cross-origin framing. – Evolutio Commented Mar 22, 2014 at 12:20
  • What's wrong with using plain old <a href=...>? – greut Commented Mar 22, 2014 at 12:20
Add a ment  | 

1 Answer 1

Reset to default 4

It works fine in fiddle. only prob i can think of is to wrap your code in ready(). This might not be causing problem in fiddle since you've selected onLoad option.

$(document).ready(function(){
    $("input[type='button']").click(function() {
        switch(this.id) {
            case 'x':window.location.href = "http://google."; break;
            case 'y': window.location.href="http://yahoo."; break;
            case 'z': window.location.href = "http://stackoverflow."; break;
        }
    });
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744802933a4594602.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信