开发者

How do I make a div with contentEditable set show a blinking cursor on page load?

开发者 https://www.devze.com 2022-12-30 04:39 出处:网络
I would just like to have an editable div that acts like a textarea that is given focus on page load (i.e. the blinking cursor is visible and typing shows up in the div开发者_StackOverflow without hav

I would just like to have an editable div that acts like a textarea that is given focus on page load (i.e. the blinking cursor is visible and typing shows up in the div开发者_StackOverflow without having to select the div with the mouse). I've tried calling focus() on the editable div, but that doesn't work.


I'm not sure it's possible to control the cursor, but you can simply focus the element:

function initPage() {
    var elEd = document.getElementById('editor');
    elEd.contentEditable=true;
    elEd.focus();
}

In Chrome, if your element with ID editor has any content then the whole content will be selected. In Firefox you don't see a cursor, but if you type after loading the page it will appear in the element. Simple example here.

0

精彩评论

暂无评论...
验证码 换一张
取 消