Can';找不到问题:javascript/jQuery

Can';找不到问题:javascript/jQuery,javascript,jquery,Javascript,Jquery,在链接的KeyDown事件中调用以下函数。 基本上,我试图向下移动表(应用程序称之为ListBox)。在第一个循环中,我尝试做的是查看他们是否首先使用鼠标在表内单击,我希望找到行值,然后从那里操作类(高亮显示) 不幸的是,现在我还没走到那一步。当屏幕加载并且我按下向下按钮时,当前行(0)的类和行(1)都发生了更改。但按下下一个向下按钮时,tru lst表示它未定义。这会导致循环中断,然后我会出现各种各样的错误 我试图实现一个,但是,我无法让它工作。但是你可以看到我试图实现的一些代码 functi

在链接的KeyDown事件中调用以下函数。 基本上,我试图向下移动表(应用程序称之为ListBox)。在第一个循环中,我尝试做的是查看他们是否首先使用鼠标在表内单击,我希望找到行值,然后从那里操作类(高亮显示)

不幸的是,现在我还没走到那一步。当屏幕加载并且我按下向下按钮时,当前行(0)的类和行(1)都发生了更改。但按下下一个向下按钮时,tru lst表示它未定义。这会导致循环中断,然后我会出现各种各样的错误

我试图实现一个,但是,我无法让它工作。但是你可以看到我试图实现的一些代码

function xKeyDown(event,ListBoxVal){


var tr_lst = $('#' + ListBoxVal).find('tr[class="LUGridRowHighlight"]');
var iCount = 0;

for (iCount = 0; iCount <= $('#' + ListBoxVal + ' tr').length; iCount++){
    if($('#' + ListBoxVal + ' tr:eq('+iCount+')').attr('id') == tr_lst.attr('id')){
        lstRow = iCount;
        break;
    }
}


if (event.keyCode == 40){
//arrow down
    if(parseInt(lstRow) < $('#' + ListBoxVal + ' tr').length)
    {
        if(parseInt(lstRow) == 0){
            document.getElementById(ListBoxVal).focus(); 
            lstRow +=1;

            document.getElementById($('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').attr('id')).focus();
            $('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');
            $('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight') .addClass('LUGridRow');
            
        }else{
            document.getElementById($('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').attr('id')).focus();
            $('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');
            $('#' + ListBoxVal + ' tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight') .addClass('LUGridRow');
            lstRow +=1;
        }
    }
...
当我试图打印出来时,它是“未定义的”

我想知道,既然我是通过jQuery操作这个类的,我是否需要添加一个.live来查找?正如我所相信的,当元素被动态地操纵时,live就开始发挥作用。有什么建议吗?

试试这个

function xKeyDown(event,ListBoxVal){

var $listBoxVal = $('#' + ListBoxVal);
var trs = $listBoxVal.find('tr');
var tr_lst = $listBoxVal.find('tr.LUGridRowHighlight');
var tr_lst_id = tr_lst.attr('id');
var iCount = 0;

for (iCount = 0; iCount <= trs.length; iCount++){
    if($listBoxVal.find('tr:eq('+iCount+')').attr('id') == tr_lst_id){
        lstRow = iCount;
        break;
    }
}


if (event.keyCode == 40){
//arrow down
    if(parseInt(lstRow) < $listBoxVal.find('tr').length)
    {
        if(parseInt(lstRow) == 0){
            $listBoxVal.focus(); 
            lstRow +=1;

            $("#"+$listBoxVal.find('tr:eq('+parseInt(lstRow)+')').attr('id')).focus();

           $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');

            $listBoxVal.find(' tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight').addClass('LUGridRow');

        }else{
            $("#"+$listBoxVal.find('tr:eq('+parseInt(lstRow)+')').attr('id')).focus();
            $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');
            $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight').addClass('LUGridRow');
            lstRow +=1;
        }
    }
...
函数xKeyDown(事件,ListBoxVal){
var$listBoxVal=$(“#”+listBoxVal);
var trs=$listBoxVal.find('tr');
var tr_lst=$listBoxVal.find('tr.LUGridRowHighlight');
var tr_lst_id=tr_lst.attr('id');
var-iCount=0;

对于(iCount=0;iCount我建议(Shankar和Jeff V)将
for
语句改为此,以提高性能和可靠性:
for(iCount=0,iMax=$('#'+ListBoxVal+'tr')).length;iCount似乎对我不起作用。我认为这与。查找有关。在第一次单击后,我正在操作CSS,现在它返回未定义(在第一次单击后-这是成功的)。您真的要处理ScroldDown列表中的keydown事件,还是需要处理MousedDown事件?在ScroldDown列表中是的。我是根据需求操作的。我在JSFIDLE上看到的是,scrool可以上下运行,但没有调用您的处理程序(未捕获引用错误:未定义xKeyDown)为什么您首先需要一个额外的处理程序来使用滚动条滚动元素?@vittore不幸的是,我只能提供一些小提琴中的代码。您是对的,滚动确实有效,我的问题与for循环有关(我想)正在阅读您的代码,但仍然无法找出原因。如果您只需要scrool,则根本不需要执行任何操作。否则,在特定的列表框项(而不是列表框)上处理键控是怎么回事?
function xKeyDown(event,ListBoxVal){

var $listBoxVal = $('#' + ListBoxVal);
var trs = $listBoxVal.find('tr');
var tr_lst = $listBoxVal.find('tr.LUGridRowHighlight');
var tr_lst_id = tr_lst.attr('id');
var iCount = 0;

for (iCount = 0; iCount <= trs.length; iCount++){
    if($listBoxVal.find('tr:eq('+iCount+')').attr('id') == tr_lst_id){
        lstRow = iCount;
        break;
    }
}


if (event.keyCode == 40){
//arrow down
    if(parseInt(lstRow) < $listBoxVal.find('tr').length)
    {
        if(parseInt(lstRow) == 0){
            $listBoxVal.focus(); 
            lstRow +=1;

            $("#"+$listBoxVal.find('tr:eq('+parseInt(lstRow)+')').attr('id')).focus();

           $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');

            $listBoxVal.find(' tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight').addClass('LUGridRow');

        }else{
            $("#"+$listBoxVal.find('tr:eq('+parseInt(lstRow)+')').attr('id')).focus();
            $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').addClass('LUGridRowHighlight');
            $listBoxVal.find('tr:eq('+parseInt(lstRow)+')').prev().removeClass('LUGridRowHighlight').addClass('LUGridRow');
            lstRow +=1;
        }
    }
...