强制使用enter,而不是点击javascript(Force to use enter instead of click in javascript)

编程入门 行业动态 更新时间:2024-10-27 01:21:12
强制使用enter,而不是点击javascript(Force to use enter instead of click in javascript)

我有一个问题:

document.querySelector('#target').click();

我用click()方法实现了这个代码,但是我想点击目标按下enter键(keycode = 13),当然用javascript不是物理的。 那么怎么可能呢?

这是一个js小提琴,用于表示它是什么样的......

https://jsfiddle.net/

当然,我想在开发人员模式下解决这个问题,所以在控制台中运行脚本。

请帮我解决这个问题。 提前致谢。

I've a problem :

document.querySelector('#target').click();

I've this code with the click() method, but I would like to click on the target pressing the enter (keycode = 13), of course with javascript not physically. So how is it possible to do?

Here is a js fiddle, for representing how does it look like...

https://jsfiddle.net/

Of course, I would like to solve this problem within the developer mode, so running the script in the console.

Please help me to solve this problem. Thanks in advance.

最满意答案

因为你使用jQuery,你可以使用:

$('#target').trigger({type: 'keypress',which: character.charCodeAt(13)});

或者:

var ev = jQuery.Event("keypress"); ev.which = 13; $("#target").trigger(ev);

希望这可以帮助。

Because you're using jquery you could use :

$('#target').trigger({type: 'keypress',which: character.charCodeAt(13)});

Or also :

var ev = jQuery.Event("keypress"); ev.which = 13; $("#target").trigger(ev);

Hope this helps.

更多推荐

problem,问题,click,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-08-07 22:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   enter   javascript   click   Force

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!