2013-04-07 55 views

回答

1

下面是一个简单的例子

HTML

<input type="text" /> 
<div class="page page120">You're on page 120</div> 
<div class="page page240">You're on page 240</div> 
<div class="page page360">You're on page 360</div> 

JS

$('input').keyup(function() { 
    $('.page').hide(); 
    $('.page'+$(this).val()).show(); 
}); 

小提琴:http://jsfiddle.net/5G8m9/

编辑

根据您的dom结构,ajax调用可能如下所示。你可以设置一个hashtag来显示那里的网页。

$('input').keyup(function() { 
    $.ajax({ 
     url: "http://yourblog.com/page/"+$(this).val() 
    }).done(function (data) { 
     location.hash = $(this).val(); 
     $('.content').html(data); 
    }); 
}); 

读到它在这里:http://api.jquery.com/jQuery.ajax/

+0

对于某些系统可能是一个前输入3位数字有点矫枉过正,可以呈现998个隐藏页面和可见的页面。 :)但解决方案也可以与ajax一起工作,所以+1 – scones 2013-04-07 11:36:01

+0

嗨,感谢,真的有点作品,并且非常简单,但我想知道是否有可能更改页面的URL?我不知道如何使用Ajax来实现结果。 – Karlgoldstraw 2013-04-07 11:49:30

0

斯文的编辑会的工作,但一个简单的(AJAX免费)的方法是:

$(document).keyup(function (e) { 
    var charCode = (96 <= e.which && e.which <= 105) 
     ? e.which - 48 
     : e.which; 
    var keyVal = String.fromCharCode(charCode); 
    if (!isNaN(parseInt(keyVal))) 
     window.location.href = '/?pageId=' + keyVal 
}); 

这将浏览器简单地重定向到www.domain.com/?pageId=<NumberPressed>。没有更多的信息,很难改善这个答案。

编辑: 这是一个基本JSFiddle

+1

键的键码不是它可能涉及的字符的字符码! – Bergi 2013-04-07 13:09:29

+0

@Bergi谢谢你指出。我修复了错误。该错误与数字键盘中的关键字符与它们所代表的unicode值不同。 – azz 2013-04-07 14:01:52

0

这是一个扩展到@ DerFlatulator的答案,让您导航到该页面

// keys and loc should be outside the scope of your event handler 
// a list of valid keys and their values (0-9 and 0-9 on numberpad) 
var keys = { "48": 0, "49": 1, "50": 2, "51": 3, "52": 4, "53": 5, "54": 6, "55": 7, "56": 8, "57": 9, "96": 0, "97": 1, "98": 2, "99": 3, "100": 4, "101": 5, "102": 6, "103": 7, "104": 8, "105": 9 }; 
// used to hold the page being typed 
var loc = []; 

$(document).keyup(function(e) { 
    if (keys[e.keyCode]) { 
     if (keys.push(keys[e.keyCode]) == 3) { 
      document.location.href = "/?pageId=" & loc.join(""); 
     } 
    } 
}); 
相关问题