python输入字母终止_如何在表单中键入字母时停止触发键盘功能?(How to stop keyboard functions from firing when the letters are ty...

如何在表单中键入字母时停止触发键盘功能?(How to stop keyboard functions from firing when the letters are typed in a form?)

我有这个脚本,当按下键盘上的“N”和“P”键时,它会触发滚动到next / prev。 我的问题是当用户以任何形式(例如搜索字段,登录字段等)正常输入这些字母时,如何阻止这种情况发生,因为当我在表格中按n例如John时,它不会写N而是触发滚动功能。 如何解决这种行为?

我的代码如下:

$(document).keydown(function (evt) {

if (evt.keyCode == 78) {

evt.preventDefault();

scrollToNew();

} else if (evt.keyCode == 80) {

evt.preventDefault();

scrollToLast();

}

});

谢谢。

I have this script which fires scrolling to next/prev when "N" and "P" keys on keyboard are pressed. My question is how to stop this from firing when the user type these letters normally in any form (such as search field, login field, and so on.) because when I press n e.g. John in a form it does not write N instead it fires the scroll function. How to fix that behaviour?

My code is below:

$(document).keydown(function (evt) {

if (evt.keyCode == 78) {

evt.preventDefault();

scrollToNew();

} else if (evt.keyCode == 80) {

evt.preventDefault();

scrollToLast();

}

});

Thank you.

原文:https://stackoverflow.com/questions/10755197

更新时间:2019-12-01 16:42

最满意答案

事件对象有一个target属性,它告诉您事件实际发生的位置(之后它会冒泡到文档级处理程序)。 因此,您可以针对表单字段元素测试evt.target ,如果匹配,则不要执行您的特殊逻辑。

可能最简单的方法是使用closest ,例如:

$(document).keydown(function (evt) {

if (!$(evt.target).closest('input, select, textarea')[0]) {

if (evt.keyCode == 78) {

evt.preventDefault();

scrollToNew();

} else if (evt.keyCode == 80) {

evt.preventDefault();

scrollToLast();

}

}

});

在那里,我问的是最接近的表单字段是从元素开始的(因为closest从你给它的元素开始)并通过祖先进行处理。 如果有非,则返回的jQuery对象将为空, [0]将返回undefined 。 因为!undefined是true ,我们将在不在表单字段中时处理你的逻辑。

The event object has a target property which tells you where the event actually occurred (after which it bubbled up to your document-level handler). So you can test evt.target against form field elements and, if it's a match, don't do your special logic.

Probably the easiest way to do that is to use closest, e.g.:

$(document).keydown(function (evt) {

if (!$(evt.target).closest('input, select, textarea')[0]) {

if (evt.keyCode == 78) {

evt.preventDefault();

scrollToNew();

} else if (evt.keyCode == 80) {

evt.preventDefault();

scrollToLast();

}

}

});

There I'm asking what the closest form field is starting with the element (since closest starts with the element you give it) and working up through ancestors. If there are non, then the returned jQuery object will be empty, and [0] will return undefined. So since !undefined is true, we'll process your logic when not in a form field.

2012-05-25

相关问答

你需要等到所有元素都褪色。 工作实例 但是,基本上只需添加一个计数器就可以知道所有元素何时消失: var hiddenElements = 0;

function fadeOut(pro){

$('.interactive ul li').each(function(i) {

$(this).find('a').delay(200*(i+1)).fadeOut(200,function(){

$('.opening-tit

...

一个

是一个容器元素,而不是一个input元素。 键盘事件只能由 , 和任何具有contentEditable属性的东西生成。 你可以试试 render: function() {

return (

contentEditable={true}

onKeyDown={this.playNote}

onKeyUp={this.stop

...

如果您只想在用户更改组合框中选定的项目时做出反应,那么最好订阅SelectionChangeCommitted 。 If you want to react only when the user change the selected item in the combo box, then it is better to subscribe to SelectionChangeCommitted.

错误信息是因为你的[String]数组不能被快速枚举为一对索引和值。 为此,您需要使用上面提到的enumerate函数。 我会建议将您的alphabet数组提升为全局或成员变量。 您可以将其标记为私有,以便在源文件外部不可见。 // A through Z

private let alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"

...

事件对象有一个target属性,它告诉您事件实际发生的位置(之后它会冒泡到文档级处理程序)。 因此,您可以针对表单字段元素测试evt.target ,如果匹配,则不要执行您的特殊逻辑。 可能最简单的方法是使用closest ,例如: $(document).keydown(function (evt) {

if (!$(evt.target).closest('input, select, textarea')[0]) {

if (evt.keyCode == 78) {

...

我怀疑你可以阻止CellValueChanged事件被触发,除了删除处理程序(事件仍将被触发,但不会有任何处理程序): private dgv_ThatCausesChanges_CellValueChanged(object sender, EventArgs e)

{

this.otherDGV.CellValueChanged -= this.dgv_OtherDGVCellValueChanged;

try // To make sure that han

...

有时您使用组合键Alt + Shift + SomeOtherLetter 。 如果您没有选择SomeOtherLetter足够快的速度,键盘将切换到Control Panel -> Regional And Language Options -> Languages -> Details... 重新启动Eclipse后将拾取默认键盘语言。 Sometimes you use combination of keys Alt+Shift+SomeOtherLetter. If you don't c

...

keypress事件是关于字符 ,而不是键。 您可以将keyCode与"|"的字符代码进行比较 ( "|".charCodeAt(0) )直接,无需担心shift键被关闭(并且它可能不在所有键盘上)。 示例 - 实时复制 | 来源 : HTML:

Try to type | in the box below.

JavaScript的: jQuery(function($) {

var ke

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
最新发布
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值