jKey jQuery plugin errors in IE6/7, attempting to run whenever any un-used key is pressed. How to combat?

孤者浪人 提交于 2019-12-11 11:03:59

问题


I'm using the jKey jQuery plugin on a current project. It just allows you to easily run a function on a key press. Here's my function call:

jQuery(document).jkey('left, right',function(key){
    if(key == 'left'){
        if (elementIndex == 0) { return; }
        question_nav(jQuery('.question-fieldset-active'), 'prev');
    } else {
        if ((elementIndex + 1) == jQuery('.question-fieldset').length) { return; }
        question_nav(jQuery('.question-fieldset-active'), 'next');
    }
});

In IE6 and 7, pressing any other key on the keyboard besides the left or right arrows throws a nasty "Error Message: 'indexOf' is null or not an object" error. Is there a way to capture all other key presses and return; on them so as to avoid this?


回答1:


Actually it's bug of jKey itself. I've found this bug, when tried to use at the project. That was classic problem with looping through array as object:

line 224: for(y in keySplit[x]) at GitHub revision

The solution is to traverse array as traditional loop:

for(var i = 0; i < keySplit.length; ++i)

So u can do it manually or get fixed version of 'jquery.jkey.js' from my Google Code revision




回答2:


Instead of just using else condition check for key == 'right' as well that might help you.

jQuery(document).jkey('left, right',function(key){
    if(key == 'left'){
        if (elementIndex == 0) { return; }
        question_nav(jQuery('.question-fieldset-active'), 'prev');
    } else if(key == 'right') {
        if ((elementIndex + 1) == jQuery('.question-fieldset').length) { return; }
        question_nav(jQuery('.question-fieldset-active'), 'next');
    }
});


来源:https://stackoverflow.com/questions/7066487/jkey-jquery-plugin-errors-in-ie6-7-attempting-to-run-whenever-any-un-used-key-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!