触发选择 contenteditable

Trigger selection of contenteditable

为什么这不是 select(即以蓝色突出显示等)内容可编辑的 <div>

注意:我使用的是 Firefox 36.0.1 (Windows 7)

$('#b').click(function() { $('#a').select().focus(); } );
<div id="a" contenteditable>Hello</div>
<div id="b">click here</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

如建议的那样here,这有效,至少对于 Firefox:

$('#b').click(function() { $('#a').select().focus(); document.execCommand('selectAll', false, null); } );
<div id="a" contenteditable>Hello</div>
<div id="b">click here</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

select() 函数不适用于内容可编辑的元素。看@ https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select

HTMLInputElement.select() 方法 select 将