English 中文(简体)
Javascript::How to identify what the character (or string) was inserted on a editable div?
原标题:

I want to know how to identify what the character inserted on a div editable... I want to see if the user type a single or double quotation mark, and give a specific class to this quote to the text after the quote... I think that is a onkey property or return... i don t know...

Anyone has a tip ?

最佳回答

The keypress event is what you want, since that is the only event from which you can gather information about the character typed. You could handle the keypress yourself in the case of a quote. The code to insert a <span> with a CSS class is not covered here. I would suggest asking another question if you need help, or perhaps reading some documentation on DOM Ranges, TextRanges and selections in IE and other browsers.

function handleKeypress(evt) {
    evt = evt || window.event;
    var charCode = evt.which || evt.keyCode;
    var charStr = String.fromCharCode(charCode);
    if (charStr == " " || charStr ==  " ) {
        // Code to insert quote character followed by <span> with CSS class
        // goes here.

        // Prevent the default action
        if (evt.preventDefault) {
            evt.preventDefault();
        } else {
            evt.returnValue = false;
        }
    }
}

var div = document.getElementById("your_div");
if (div.addEventListener) {
    div.addEventListener("keypress", handleKeypress, false);
} else if (div.attachEvent) {
    div.attachEvent("onkeypress", handleKeypress);
}
问题回答

I m not sure I entirely understand what you are trying to do but it seems that you wish to capture user input into a div with the contentEditable attribute. If I were using Mootools and Firebug I would begin with the following::

$( idOfEditableDiv ).addEvent( keydown , function(event) {
    console.log(event.key);
    });

That will print into the Firebug console any key that is pressed inside the content editable div. So if I press the a key, a will be printed. This can be useful if you are looking to capture input that doesn t have an obvious value such as the Caps Lock key. Logging the entire event with console.log(event) can give you even more useful information.

When you have identified which keys you wish to capture (say the a and b keys), then do the following:

$( idOfEditableDiv ).addEvent( keydown , function(event) {
    if(event.key ==  a  || event.key ==  b ) {
        //do stuff here if the a or b key was pressed
        }
    });

Sometimes you might want to do stuff if the a key was pressed and other stuff if the b key was pressed. In that case, do the following:

$( idOfEditableDiv ).addEvent( keydown , function(event) {
    if(event.key ==  a ) {
        //do stuff here if the a key was pressed
        }
    else if(event.key ==  b ) {
        //do stuff here if the b key was pressed
        }
    });

In case you aren t familiar with Mootools, you would need to wrap all your Mootools code in a domReady event like this:

window.addEvent( domready , function() {
    $( idOfEditableDiv ).addEvent( keydown , function(event) {
        if(event.key ==  a ) {
            //do stuff here if the a key was pressed
            }
        else if(event.key ==  b ) {
            //do stuff here if the b key was pressed
            }
        });
    });

More information about Mootools Events





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签