javascript 如何模糊 contentEditable=true 的 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4878713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I blur a div where contentEditable=true?
提问by Babiker
How can I blur/focusout a div where contentEditable=true? I have attempted $(elm).blur()and $(elm).attr('contentEditable', false);
如何在contentEditable=true 的位置模糊/聚焦 div ?我已经尝试$(elm).blur()和$(elm).attr('contentEditable', false);
采纳答案by Roman Resh
$("div[contentEditable]").blur()to blur all contentEditable elements.
$("div[contentEditable]").blur()模糊所有 contentEditable 元素。
$("#elementId").blur()to blur an element by its id.
$("#elementId").blur()通过它的 模糊一个元素id。
回答by Marek Suscak
I would add another solution that is based on Omnicon's answer which is correct BTW. I have forked his fiddle and updated it.
我会添加另一个基于 Omnicon 答案的解决方案,BTW 是正确的。我已经分叉了他的小提琴并更新了它。
The trick is to remove all ranges after calling blur:
诀窍是在调用模糊后删除所有范围:
$(".editable").blur();
window.getSelection().removeAllRanges();
回答by Omnicon
The suggested solution by Roman Resh doesn't work when you click into the element and hit enter immediately after. It won't blur but create line-breaks/divs.
当您单击元素并在之后立即按 Enter 时,Roman Resh 建议的解决方案不起作用。它不会模糊但会创建换行符/div。
It is possible to prevent this behaviour entirely.
完全可以防止这种行为。
$('<div class="temp-contenteditable-el" contenteditable="true"></div>')
.appendTo('body').focus().remove();
$('<div class="temp-contenteditable-el" contenteditable="true"></div>')
.appendTo('body').focus().remove();
回答by halfbit
Appending to all answers, when using something like
附加到所有答案,当使用类似
$(".editable").blur()
think about blurring only focused, like
考虑仅聚焦模糊,例如
$(".editable:focus").blur()
If not you can get into nice probs sometimes
如果不是,你有时会遇到很好的问题

