简体   繁体   English

contentEditable div 中的 Span 标签需要按键事件

[英]Span tag inside contentEditable div needs keypress event

I have the following div in my html code:我的 html 代码中有以下 div:

<div id="expanderInput" tabIndex="1" contentEditable></div>

I'm using a contentEditable div as a simple, cross-browser method of making an auto-expanding textbox.我使用contentEditable div 作为制作自动扩展文本框的简单、跨浏览器方法。 I'm also doing input validation on what the user enters into this div.我也在对用户输入到这个 div 的内容进行输入验证。 It is supposed to be a list of email addresses separated by commas.它应该是由逗号分隔的电子邮件地址列表。 When the user tries to submit incorrect information, my Javascript chunks up the input and highlights in red all the incorrect chunks.当用户尝试提交不正确的信息时,我的 Javascript 将输入分块并以红色突出显示所有不正确的块。

//split the address into the comma-separated chunks
var chunks = splitForward(forwardsTo);

//Grab each bad chunk one at a time and stick it in a span with a red-colored text
for(var i = 0; i < invalids.length; i++)
{
    var current = chunks[invalids[i]];
    current = '<span class="highlighted">' + current + '</span>';
    chunks[invalids[i]] = current;
}
$("#expanderInput").html(chunks.join());
initHighlighted();

The array 'invalids' holds the indexes of all the bad chunks.数组 'invalids' 包含所有坏块的索引。 Everything up to this point works fine.到目前为止一切正常。 But once the user starts typing inside the red text I need the red to disappear, but just in that span.但是一旦用户开始在红色文本中输入,我需要红色消失,但只是在那个范围内。 For example, if there are two bad chunks, each highlighted in red, and the user starts fixing one, the other needs to stay red.例如,如果有两个坏块,每个都用红色突出显示,用户开始修复一个,另一个需要保持红色。

I've tried attaching an event listener to the spans:我尝试将事件侦听器附加到跨度:

initHighlighted = function()
{
    $(".highlighted").keypress(function()
    {
        alert("It works!");
    });
};

But the event never happens, even when the user edits the text in red.但是该事件永远不会发生,即使用户编辑红色文本也是如此。 Using browser's developer's tools, I can see that the event handler is there, it is just never set off.使用浏览器的开发人员工具,我可以看到事件处理程序就在那里,只是永远不会被触发。 Is it the contentEditable attribute on the div causing the span from receiving the event?是 div 上的contentEditable属性导致跨度接收事件吗? I've also tried making the spans themselves contentEditable , but the behavior is still the same.我也试过让跨度本身contentEditable ,但行为仍然相同。

Unless I'm mistaken, this should solve your problem :除非我弄错了,否则这应该可以解决您的问题:

initHighlighted = function()
{
    $(".highlighted").live("keypress",function()
    {
        alert("It works!");
    });
};

Because your spans are added after the initial loading of the DOM the keypress event listeners haven't been attached as there was nothing to attach them to.因为您的跨度是在 DOM 初始加载后添加的,所以没有附加按键事件侦听器,因为没有任何东西可以附加它们。 Jquery's live sorts this out for you by attaching those listeners to, in this case, anything with the class 'highlighted' no matter when they are added to the DOM. Jquery 的live会通过将这些侦听器附加到任何带有“突出显示”类的内容(无论何时将它们添加到 DOM 中)来为您解决这个问题。

Read the documentation on the Jquery site to get a much better explanation than I could give you : http://api.jquery.com/live/阅读 Jquery 站点上的文档以获得比我能给你的更好的解释: http : //api.jquery.com/live/

EDIT : My apologies for not reading your question properly and realising that your were attaching the keypress listener after after the 'highlighted' spans were added.编辑:我很抱歉没有正确阅读您的问题并意识到您在添加“突出显示”跨度后附加了按键侦听器。

Have you read this though :你有没有读过这个:

Keyboard events for child elements in a contenteditable div? contenteditable div中子元素的键盘事件?

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

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