简体   繁体   中英

Trouble converting Javascript array into JQuery for IE7 compatibility

I am trying to convert a line in Javascript so that the functionality works in IE7. Below is the code I am trying to convert. Obviously IE doesn't like the class attribute being set which is where the issue occurs. So my solution was to translate it using JQuery for browser compatibility. But I am having trouble converting the array into something JQuery understands.

This is the code:

    ulAccNm.children[iVal].setAttribute("class", "show");

I have tried the following, but it doesn't work correctly.

  $(ulAccNm).children(jQuery.inArray("iVal")).addClass('show').removeClass('hide');

The variable of iVal translates into the total number of children to the parent and loops through each to determine to add a class or not. Any help would be greatly appreciated.

I'm guessing a lot here, but I think this will translate:

$($(ulAccNm).children()[iVal]).addClass('show').removeClass('hide');

However, if you want to do this to ALL the children, you don't need an explicit loop with iVal . Just use

$(ulAccNm).children().addClass('show').removeClass('hide');

You may just want to try changing your loop structure from a for loop to the jQuery.each method:

$(ulAccNm).children().each(function () {
    // Decide if the class needs to be changed
    if (needsToBeChanged)
        $(this).addClass('show').removeClass('hide');
})

通过使用Kevin B建议的.children()。eq(ival)结束修复,这使其工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM