也可以javascript preSS的我回车键? [英] Can Javascript press the Enter key for me?
问题描述
还有,我想继续打下去就进入我不在的时候一个网站。是否有可能做这样的事
There's a site that I want to continue to hit enter on while I'm away. Is it possible to do something like
setInterval(function(){
//have javascript press the button with a certain id
},100);
我想的只是把在智能搜索栏,这样它会运行code。
I was thinking of just putting that in the smart search bar so it would run the code.
推荐答案
好吧pressing输入被触发事件。你必须弄清楚他们是听哪个事件侦听器。我将在下面的例子中使用 KEYUP
:
Well pressing enter is triggering an event. You would have to figure out which event listener they are listening to. I'll use keyup
in the following example:
假设报
是你想输入是pressed对元素的变量。我不知道你怎么会得到这个元素,但我敢肯定,你知道的。
Assume el
is the variable for the element you want enter to be pressed on. I'm not sure how you going to get that element but I'm sure you know.
var evt = new CustomEvent('keyup');
evt.which = evt.keyCode = 13;
el.dispatchEvent(evt); //This would trigger the event listener.
有知道的方式来真正模拟硬件的动作。它只是触发事件侦听器。
There's know way to actually simulate a hardware action. It just triggers the event listener.
例如调用 el.click()
只调用事件侦听器的回调,实际上并不pressing的关键。
For example calling el.click()
is only calling the callback of the event listener, not actually pressing the key.
所以 EVT
是与以下事件
变量:
所以,你知道当你添加一个事件监听到一个元素的第一个参数是事件对象。
So you know how when you add an eventListener to an element the first argument is the event object.
el.addEventListener('keyup', function(event) {
//do whatever. Here event == evt. When calling dispatchEvent on el.
});
如果使用的编程:
el.onkeyup = function(event) {
//do whatever.
}
这是非常容易的。
It's surprisingly easy.
就叫 el.onkeyup(EVT);
由于的onkeyup
是一个函数。
为什么要用自定义事件
而不是的KeyboardEvent
,因为新的KeyboardEvent('KEYUP )
返回与该属性的对象这
和键code
,可以'无需使用T为重写 Object.defineProperty
或 Object.defineProperties
Why did I use CustomEvent
instead of KeyboardEvent
because new KeyboardEvent('keyup')
return's an object with the properties which
and keyCode
that can't be rewritten without the use of Object.defineProperty
or Object.defineProperties
这篇关于也可以javascript preSS的我回车键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!