简体   繁体   English

如何在 JavaScript 中的 onclick 之后立即执行 function?

[英]How to make a function execute immediately after onclick in JavaScript?

I want to change the caret position whenever the user clicks the mouse within a textbox.每当用户在文本框中单击鼠标时,我想更改插入符号 position。 This is because I want to disable the possibility of the user changing the caret position;这是因为我想禁用用户更改插入符号 position 的可能性; it has to stay constant and unchangeable.它必须保持不变和不变。

But it seems that the function that onclick triggers is first executed about 0.3 seconds after the onclick actually occurs.但似乎onclick触发的 function 是在onclick实际发生后约 0.3 秒首次执行。 There is a delay.有延迟。

    input.onclick = function(){
        input.setSelectionRange(0, 0);
    }

input is the variable for the input field. input是输入字段的变量。

You might find more control with mouse events by also using the onmousedown event.通过使用onmousedown事件,您可能会发现对鼠标事件的更多控制。 onclick waits until the mouse button comes up. onclick等到鼠标按钮出现。 By the looks of it, could basically copy/paste another copy of this code, and just change onclick to onmousedown .从外观上看,基本上可以复制/粘贴此代码的另一个副本,只需将onclick更改为onmousedown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM