在 JavaScript 中移动鼠标指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2322603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:46:10  来源:igfitidea点击:

Move mouse pointer in JavaScript

javascript

提问by Brian

Is it possible to move the mouse so that it is positioned inside a text input using JavaScript?

是否可以移动鼠标使其位于使用 JavaScript 的文本输入内?

回答by jasonbar

I don't know about moving the actual rendered mouse, but could you just set the focus to the element?

我不知道移动实际渲染的鼠标,但你能把焦点设置在元素上吗?

document.getElementById('the_text_input_id').focus()

回答by JP Silvashy

Please see this question:

请看这个问题:

Mouse move on element

鼠标在元素上移动

Besides that, I think you are committing major design mistake by taking control of any of the users input in any way (maybe besides setting the focus of a form element)

除此之外,我认为您通过以任何方式控制任何用户输入(也许除了设置表单元素的焦点)而犯了重大的设计错误

回答by Mic

Here is a function that select text in an input or textarea:

这是一个在输入或文本区域中选择文本的函数:

    function textSelect(inp, s, e) {
        e = e || s;
        if (inp.createTextRange) {
            var r = inp.createTextRange();
            r.collapse(true);
            r.moveEnd('character', e);
            r.moveStart('character', s);
            r.select();
        }else if(inp.setSelectionRange) {
            inp.focus();
            inp.setSelectionRange(s, e);
        }
    }

To place the cursor at the 12th position:

将光标置于第 12 个位置:

textSelect(document.getElementById('theInput'), 12);

To select a portion of the input field:

要选择输入字段的一部分:

textSelect(document.getElementById('theInput'), 12, 15);

回答by Warty

It would be a huge [security?] issue if they allowed for something like this. Imagine: you have a setInterval(function(){moveMouseToTopLeftCorner and alert garbage}, 1)...
The user would have his mouse moved to the top left. And then alert would show up [which could be closed with enter].. upon which an alert would immediately pop up again.

如果他们允许这样的事情,那将是一个巨大的 [安全?] 问题。想象一下:你有一个 setInterval(function(){moveMouseToTopLeftCorner and alert Garbage}, 1) ...
用户会将他的鼠标移到左上角。然后警报会出现[可以用回车关闭]..然后会立即再次弹出警报。

You'd actually have to use your keyboard to open taskmanager and kill the browser >_>

您实际上必须使用键盘打开任务管理器并关闭浏览器 >>>

However, it is probably possible with ActiveX [although thats IE only... and dumb]

但是,使用 ActiveX 可能是可能的 [虽然那只是 IE ......而且愚蠢]