English 中文(简体)
Build a Google Suggest box and respond to keyboard events without jQuery
原标题:

I need to build a Google Suggest style drop-down box, but unfortunately am unable to use jQuery or Prototype due to various licensing restrictions. Pretty much the only thing I can use is public-domain stuff that I can modify and am not required to attribute ownership of someone else to.

In any case, I can handle the AJAX stuff fine, and the server-side scripting. Where I m stuck is how to make Javascript respond to keyboard events:

  • Select the next item in the list when the user hits the down arrow key
  • Select and fill the text box with the selected item when they hit [Enter]
  • Close the suggest box when they hit [Esc]
  • etc. etc. etc.

Thoughts? Examples?

问题回答

Not exactly what you are looking for but might be of interest. A fancy Apple.com-style search suggestion using jQuery.

You can use this code to attach/detach events (compatible with both IE and W3C event models, so should work in all browsers):

events = {

    addEvent: function(obj, e, handler) {
        if (!obj._attachedEvents)
            obj._attachedEvents = new Array();
        if (obj._attachedEvents[e])
            this.removeEvent(obj, e);
        obj._attachedEvents[e] = handler;

        if (obj.addEventListener) 
            obj.addEventListener(e, handler, false);
        else if (obj.attachEvent) 
            obj.attachEvent( on  + e, handler);
    },

    removeEvent: function(obj, e) {
        if (!obj._attachedEvents || !obj._attachedEvents[e])
            return;

        if (obj.removeEventListener) 
            obj.removeEventListener(e, obj._attachedEvents[e], false);
        else if (obj.detachEvent) 
            obj.detachEvent( on  + e, obj._attachedEvents[e]);
        obj._attachedEvents[e] = null;
    },

    cancelEvent: function(e) {
        e = e || window.event;

        if(e.preventDefault)
            e.preventDefault();
        if(e.stopPropagation) 
            e.stopPropagation(); 
        e.cancelBubble = true;
        e.returnValue = false;
        return false;
    },

    getTarget: function(e) {
        return e.srcElement || e.target;
    }
};

and then in the handler you can see what key was pressed and do whatever action you want in response to that.

Hope it helped, Andrey

Take a look at js-hotkeys. I ve used this library in many projects. It works in all A-grade browsers which allows you to catch key-events, and the license is pretty liberal.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签