无法“聚焦”可编辑的“div”元素

Not able to `focus` editable `div` element

我想将焦点设置到我的 contentEditable div 元素之一。但不工作。

这是我的代码:

$(document).click(function () {
    $('div').focus();
});
div{
    border:1px solid red;   
}
<div contentEditable >&nbsp;</div>

这是一个不使用 jQuery 的工作代码段(您不会在代码段中加载)。我还添加了一个 ID 以获得正确的 div.

document.getElementById('edit-me').focus();
div { border:1px solid red; }
<div id="edit-me" contentEditable>&nbsp;</div>

导入jquery!耶!

$(document).ready(function () {
    //I replaced "click" with "ready" because think that you want to focus once loaded, not once you click somewhere in the document.
    //"click" can still be used if you want to focus once you clicked somewhere in your window
    $('div').focus();
});
div{
    border:1px solid red;   
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div contentEditable >&nbsp;</div>

您应该等到整个文档加载完毕后再执行 jQuery。

$(document).ready(function () {
    $('div').click(function () {
        $('div').focus();
    })
});
div{
    border:1px solid red;   
}
<div contentEditable >&nbsp;</div>