Javascript 为什么我的代码不起作用?
我正在使用javascript,当我按下左键或右键时,联系人信息应该会改变。但是当我按下左键或右键时什么也没发生,为什么Javascript 为什么我的代码不起作用?,javascript,Javascript,我正在使用javascript,当我按下左键或右键时,联系人信息应该会改变。但是当我按下左键或右键时什么也没发生,为什么 var currentIndex = 0; var contact1 = {}; contact1.name = "Fred"; contact1.phone = "555-555-5555"; contact1.birthday = "1/1/1970"; contact1.imageURL = "https://images.code.org/53a5bdfb7f4e
var currentIndex = 0;
var contact1 = {};
contact1.name = "Fred";
contact1.phone = "555-555-5555";
contact1.birthday = "1/1/1970";
contact1.imageURL =
"https://images.code.org/53a5bdfb7f4ec83174756c8e094558fd-image-
1449518131328.png";
var contact2 = {};
contact2.name = "Steve";
contact2.phone = "609-889-4738";
contact2.birthday = "3/15/1992";
contact2.imageURL =
"data:image/jpeg;base64,";
var contact3 = {};
contact3.name = "Sarah";
contact3.phone = "304-812-4509";
contact3.birthday = "6/23/2001"
contact3.imageUrl = "https://3.bp.blogspot.com/-jIOlaKwINSQ/Wni55";
var contacts = [];
appendItem(contacts, contact1);
appendItem(contacts, contact2);
appendItem(contacts, contact3);
console.log(contacts);
showCurrentContact(setText("contactInfo","Name: "+
contacts[currentIndex].name + "Phone: "+contacts[currentIndex].phone
+"Birthday: " +contacts[currentIndex].birthday));
onEvent("viewContactsScreen", "keydown", function(event) {
if(event.key == "Left"){
currentIndex--;
currentIndex = wrap(currentIndex, 0, contacts.length-1);
showCurrentContact();
} else if (event.key == "Right"){
currentIndex++;
currentIndex = wrap(currentIndex, 0, contacts.length-1);
showCurrentContact();
}
});
谢谢 看起来您没有正确使用
事件键
尝试使用event.key==“ArrowLeft”
和event.key==“ArrowRight”
没有人帮忙很难。还要报告抛出的任何错误。花几分钟的时间复习一下,为具体问题设置一个合适的标题是很好的。如果可以,请使用JSFIDLE来放置整个代码,而不仅仅是js代码。@pmirnd请不要要求JSFIDLE,因为指向第三方站点的链接可能会随着时间的推移而消失。代码可以/应该插入,就在问题的这里作为“代码片段”。