取消选择contenteditable元素 [英] Deselect contenteditable element

查看:161
本文介绍了取消选择contenteditable元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除可信范围内的选择。我的代码是:

 < h1 contenteditable> Text< / h1> 
< input type =textvalue =Text/>< br />
< input type =buttononclick =value ='取消选择输入'>
< input type =buttononclick =$('h1')。blur()value ='取消选择h1'>

我自动选择 h1 的文字来自 document.execCommand('selectAll',false,null);



删除选择输入有效,但它不适用于 h1 。我怎样才能做到这一点?



检查



如您所见,我只需点击取消选择h1 并且文本仍处于选中状态。我正在使用Chrome。

解决方案

这里的答案解决了我的问题,但由于某种原因它被删除了。就是这样:



替换 $('h1')。blur() window.getSelection()。removeAllRanges();修复它。


I am trying to remove selection inside a contenteditable field. The code I have is this:

<h1 contenteditable>Text</h1>
<input type="text" value="Text" /><br/>
<input type="button" onclick="" value='Deselect input'>
<input type="button" onclick="$('h1').blur()" value='Deselect h1'>

And I'm auto-selecting the h1's text via document.execCommand('selectAll', false, null);

Removing the selection for the input works, but it does not work for the h1. How can I achieve this?

Check this fiddle


this photo shows how it behaves for me:

As you can see I just clicked deselect h1 and the text is still selected. I'm using Chrome.

解决方案

An answer here solved my problem, but it got deleted for some reason. It was this:

Replacing $('h1').blur() with window.getSelection().removeAllRanges();" fixed it.

这篇关于取消选择contenteditable元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆