开发者

Use JavaScript to place cursor in a Chrome text input element

开发者 https://www.devze.com 2022-12-30 21:21 出处:网络
I\'ve been working on sending text to an input element in Chrome and I noticed that, unlike in IE or FF, calling .focus() does not put the curs开发者_开发问答or in the text area. So does anyone know o

I've been working on sending text to an input element in Chrome and I noticed that, unlike in IE or FF, calling .focus() does not put the curs开发者_开发问答or in the text area. So does anyone know of an alternative for placing the cursor on a Chrome input element?


I have no issues either. Here is an simple HTML file I have that works in Chrome:

<html>
<body>

<textarea id="ta1"></textarea>
<br />
<textarea id="ta2"></textarea>
<br />
<textarea id="ta3"></textarea>

<script>
document.getElementById("ta2").focus();
</script>

</body>
</html>

Are you putting the script section above or below the textareas?


Here's some of the code I'm using:

var event = document.createEvent("HTMLEvents");

event.initEvent("focus", true, true);

element.dispatchEvent(event);

0

精彩评论

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