内容可编辑+ selectAll:Firefox 不允许在动态生成的内容上进行键盘输入
我在 Firefox 中遇到问题(其他浏览器似乎工作正常),动态生成的元素包含 contenteditable="true"
属性:
如果我 selectAll
(动态地,或者用我的鼠标),Firefox 不允许键盘输入。
请参阅我的jsFiddle 示例以供参考。这似乎只影响 Firefox。
$(document).ready(function(){
$('.edit').live('dblclick', function () {
document.execCommand('selectAll',false,null);
});
$('#live').append('<p class="edit" contenteditable="true">This content is generated. Firefox will not allow keyboard input when "ALL" is selected.</p>');
});
编辑: 这是我正在开发的实际应用程序(请原谅灰尘): cr8.me/app/ff.html - 我想要的是单击(双击以选择所有文本)注释、类别或计划标题来编辑它。它对任何人都有效吗?也许这只是我的配置 - 或者糟糕的脚本。第 137、572 行是相关的。
I'm having a problem in Firefox (other browsers seem to work fine) with dynamically generated elements containing a contenteditable="true"
attribute:
If I selectAll
(either dynamically, or with my mouse), Firefox won't allow keyboard input.
Please see my jsFiddle Example for reference. This appears to only affect Firefox.
$(document).ready(function(){
$('.edit').live('dblclick', function () {
document.execCommand('selectAll',false,null);
});
$('#live').append('<p class="edit" contenteditable="true">This content is generated. Firefox will not allow keyboard input when "ALL" is selected.</p>');
});
EDIT:
Here is the actual app I'm working on (pardon the dust): cr8.me/app/ff.html - What I'm wanting is to click (double-click to select all text) a Note, Category, or Plan Title to edit it. Does it work for anyone? Maybe it's just my configuration - or poor scripting. Lines 137, 572 are relevant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,Firefox 在 span 元素上的 contenteditable 方面存在问题(我假设其他 display: inline 元素也是如此,尽管我还没有测试过)。
只需用 div 替换 span 即可解决问题。更重要的是,div 可以使用 css 设置“display: inline”属性,并且仍然可以正常工作。
在此处查看工作示例:http://jsfiddle.net/6sTJh/5/。
带有标签“Add buggy”的按钮动态添加了一个带有 contenteditable 的跨度,但它没有按预期工作,而“Addworking”按钮则附加了带有 contenteditable 属性的 div,并且它工作得很好。
你的应用程序也是如此 - 当我用 div 替换所有可内容编辑的范围时,编辑在 Firefox 3.6 和 Firefox 6.0 中工作得很好。
旁注:
至于你的应用程序代码 - 不要在多个节点上使用相同的id(就像你在每个笔记上使用相同的id“note-title”一样),否则你可能会从不同的节点上得到意想不到的行为浏览器。
一般规则是一页上只能有一个具有给定 id 的元素。使用class对多个元素进行“分组”并稍后选择它们。
Apparently Firefox has issues with contenteditable on span elements (I'm assuming that's the case with other display: inline elements, though I've not tested it).
Problem can be solved with simply replacing spans with divs. What's more - that divs can have "display: inline" property set on them using css and still working fine.
Check the working example here: http://jsfiddle.net/6sTJh/5/.
The button with label "Add buggy" dynamically adds a span with contenteditable and its not working as expected, while button "Add working" appends div with contenteditable attribute and it's working just fine.
So is your app - when I replaced all the contenteditable spans with divs, the editing is working just fine in firefox 3.6 and firefox 6.0.
Side note:
As to your application code - don't use the same id on multiple nodes (like you're doing with the same id "note-title" on every note) or you can get unexpected behavior from various browsers.
Generale rule is that you can have only one element with a given id on one page. Use class to "group" more than one elements and select them later on.