简体   繁体   English

Javascript ::如何识别可编辑div上插入的字符(或字符串)?

[英]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... 我想知道如何识别div可编辑字符上插入的字符...我想看看用户是否键入单引号或双引号,并在引号后的文本中为此引号指定特定的类...我认为这是一个onkey属性或返回...我不知道...

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. keypress事件是您想要的,因为这是唯一可以收集有关键入字符的信息的事件。 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. 这里不介绍使用CSS类插入<span>的代码。 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 . 我建议您问另一个问题,如果您需要帮助,或者阅读一些有关DOM RangesTextRangesIE其他浏览器中的选择的文档。

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. 我不确定我是否完全理解您要执行的操作,但似乎您希望将用户输入捕获到具有contentEditable属性的div中。 If I were using Mootools and Firebug I would begin with the following:: 如果我使用MootoolsFirebug,我将从以下内容开始:

$('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. 它将在内容可编辑div中按下的所有键打印到Firebug控制台中。 So if I press the 'a' key, 'a' will be printed. 因此,如果按“ a”键,将打印“ a”。 This can be useful if you are looking to capture input that doesn't have an obvious value such as the Caps Lock key. 如果您希望捕获没有明显值的输入(例如Caps Lock键),这将很有用。 Logging the entire event with console.log(event) can give you even more useful information. 使用console.log(event)记录整个事件可以为您提供更多有用的信息。

When you have identified which keys you wish to capture (say the a and b keys), then do the following: 确定要捕获的键(例如a和b键)后,请执行以下操作:

$('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. 有时,如果按a键,可能要做一些事情,如果按b键,则可能要做其他事情。 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: 如果您对Mootools不熟悉,则需要将所有Mootools代码包装在domReady事件中,如下所示:

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 有关Mootools活动的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM