我正在尝试使用JavaScript在Safari中模拟键盘事件.
我试过这个:
var event = document.createEvent("KeyboardEvent");
event.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 115, 0);
Run Code Online (Sandbox Code Playgroud)
......还有这个:
var event = document.createEvent("UIEvents");
event.initUIEvent("keypress", true, true, window, 1);
event.keyCode = 115;
Run Code Online (Sandbox Code Playgroud)
但是,在尝试这两种方法之后,我遇到了同样的问题:在执行代码之后,事件对象的keyCode/ whichproperties设置为0,而不是115.
有谁知道如何在Safari中可靠地创建和发送键盘事件?(如果可能的话,我宁愿用纯JavaScript实现它.)
我正在寻找创建一个表单,按Enter键导致焦点转到页面上的"下一个"表单元素.我一直在网上找到的解决方案是......
<body onkeydown="if(event.keyCode==13){event.keyCode=9; return event.keyCode}">
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎只适用于IE.所以这个问题的真正含义是,是否有人知道适用于FF和Chrome的解决方案?另外,我宁愿不必将onkeydown事件添加到表单元素本身,但如果这是唯一的方法,那就必须这样做.
这个问题类似于问题905222,但在我看来,这个问题值得我自己提出.
编辑:另外,我已经看到人们提出这个问题并不好,因为它与用户习惯的形式行为不同.我同意!这是客户要求:(
嗨朋友们, 我正在做一个小任务,即让用户在输入按键时对html元素进行tabindex.
作为jquery的新手,我已经编写了一些代码,在我看来它会起作用,但是它有一些问题.
初步调查结果
罪魁祸首代码,它不起作用,因为Msg lablel中的输出是"未定义"
$('*').attr('tabindex').id
Run Code Online (Sandbox Code Playgroud)

代码如下,我甚至创建了一个JSFiddle.
JQuery的
$(document).ready(function (eOuter) {
$('input').bind('keypress', function (eInner) {
if (eInner.keyCode == 13) //if its a enter key
{
var tabindex = $(this).attr('tabindex');
tabindex++; //increment tabindex
//after increment of tabindex ,make the next element focus
$('*').attr('tabindex', tabindex).focus();
**//Msg Label**
//Just to print some msgs to see everything is working
$('#Msg').text( this.id + " tabindex: " + tabindex
+ " next element: " + $('*').attr('tabindex').id);
return false; // to cancel out Onenter …Run Code Online (Sandbox Code Playgroud) 假设我有一个充满可聚焦元素的文档,要么是因为它们天生具有可聚焦性(如<input type="text">),要么因为它们具有tabindex="0"等等.
现在让我们说我的文档中有一部分要显示为模式对话框,我不希望用户被对话框外的任何东西分心.我想tab键只能通过对话框的容器元素内的可聚焦元素循环.最简单的方法是什么?
如果可能的话,我正在寻找一种解决方案,它不关心对话框的内容或页面的其余部分是什么,也不会尝试修改它们.也就是说,我不想让对话框之外的元素不可聚焦.首先,这需要做出可逆的改变并跟踪状态.其次,这需要了解元素可以聚焦的所有可能方式.这让我觉得凌乱,脆弱,不易退缩.
我的第一次尝试看起来像这样,但只能在向前方向上工作(按Tab键).它不能反向工作(按Shift + Tab键).
<div>Focusable stuff outside the dialog.</div>
<div class="dialog" tabindex="0">
<!-- Focus should be trapped inside this dialog while it's open -->
<div class="content">
Form contents and focusable stuff here.
</div>
<div class="last-focus" tabindex="0" onfocus="this.parentNode.focus()"></div>
</div>
<div>More focusable stuff outside the dialog.</div>
Run Code Online (Sandbox Code Playgroud)
我宁愿看到纯JavaScript解决方案.如果有一种方法可以使用诸如jQuery之类的库来执行此操作,我更喜欢指向执行此操作的库代码的链接.
在我用 Ionic 和 Angular 8 构建的 PWA 中,我需要处理回车键以移动到下一个元素。我怎样才能用(keyup.enter)实现它。请帮我。
我有一个带有 3 个文本框和 1 个单选按钮列表的 ASP.Net 页面。4 个控件中的每一个都有
class="tabbable"
Run Code Online (Sandbox Code Playgroud)
在其定义中。这是完整的标记:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="approvalacctprocess.ascx.cs" Inherits="cmc.workflow.ui.ApprovalAcctProcess" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register tagprefix="CMC" Tagname="ApprovalComments" src="~/workflow\ui\ApprovalComments.ascx" %>
<script src="../../Scripts/jquery-1.4.1.js"></script>
<asp:Panel ID="pnlApprovalAC" CssClass="STDPANEL" HorizontalAlign="Center" Runat="server" Width="550">
<TABLE cols="2" width="520">
<TR>
<TD class="FLDLABEL" style="VERTICAL-ALIGN: top">Client Number</TD>
<TD>
<asp:TextBox id=txtclnum style="VERTICAL-ALIGN: top" Width="300" Runat="server" CssClass="FLDVALUE" TabIndex="0" onchange="MoveNext(this);" Text='<%# Property["clnum"] %>' MaxLength="14" AutoPostBack="True" class="tabbable"></asp:TextBox>
<asp:RegularExpressionValidator id="rxClNum" ValidationExpression="^[0-9]+[	]*$|Clt Number TBD" ErrorMessage="Client Number consists of up to 14 numbers"
ControlToValidate="txtclnum" runat="Server"></asp:RegularExpressionValidator></TD>
<TR>
<TD class="FLDLABEL" style="VERTICAL-ALIGN: top">Matter Number (5-6 digit)</TD> …Run Code Online (Sandbox Code Playgroud)