2

(我使用before()jquery 中的函数将新<p>元素附加到 div 层。

$('#AddParagraphButton').click(function() {
    $('#TheLayer').before('<p contentEditable='true'>Some text...</p>');    
});



在这里,我设置keypress了插入<br>标签的功能。

$('p').keypress(function(e){
    if(e.which == 13){
       e.preventDefault();  
       document.execCommand('insertHTML', false, '<br/>');
    }
});


这工作正常(br 标记插入),直到调用 append 函数并<p>添加一个新函数。如何让 livequery 取消绑定按键事件并再次绑定?


编辑:标签<p>具有contentEditable属性。我这样做是因为<br>标签包含在 div 中,我只想要<br>标签

4

1 回答 1

1

您是否考虑过使用内置live()功能?...

描述:将处理程序附加到与当前选择器匹配的所有元素的事件,现在和将来。

$('p').live("keypress", function(e){
    e.preventDefault();
      if(e.which == 13){
         document.execCommand('insertHTML', false, '<br/>');
    }
});
于 2010-12-30T21:10:31.747 回答