为什么我的代码不起作用?

时间:2018-05-28 22:46:46

标签: 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/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();
  }
});

谢谢!

1 个答案:

答案 0 :(得分:2)

您好像没有正确使用event.key

尝试使用event.key == "ArrowLeft"event.key == "ArrowRight"

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key