开发者

Initiating key strokes in JavaScript

Let's say I have a web page with a header menu, when I click the header menu, it calls a servlet that creates the sidebar. Is it possible that without using the document.getElementById? And just simulate keystrokes tab and enter开发者_开发知识库 via JavaScript so I don't have to click the menu to view the sidebar?


Could you describe what you want to achieve a bit more?

What I understood is that you want to be able to show ( and may be also hide) the sidebar with the tab button.

You could use the .keypress() function in jQuery - http://api.jquery.com/keypress/

Also check out this tutorial on Nettuts, I think it may be useful for you - http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-keypress-navigation-using-jquery/


You can use the attribute tabindex on the elements that makes your menu.

eg: <ul tabindex="1">... and set the focus on the first one when opening the page.

They will act as form field when you press tab.

Then for the enter place a single onkeyup listener on a parent node common to all menus items:

menuParent.onkeyup = function(ev){
  var selectedMenu = ev.target || ev.srcElement,
      keycode = ev.keyCode;
      if(keycode === 13){
        //the user pressed enter
      }
  ...
}


You can do what you want using JavaScript, but there's a much easier way to do it than by simulating keystrokes.

I am assuming that what happens when you click the link is that a JavaScript function is called, which causes the submenu to appear. All you need to do is to find out what that function call is (let's say it's called "callTheFunction"), and then call it onload, like this:

<script type="text/javascript">
window.onload=callTheFunction;
</script>

Hopefully that will give you the idea. If you need any more help, please provide a URL or code sample.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜