IE 11 addClass + removeClass

前端 未结 2 519
我在风中等你
我在风中等你 2021-01-25 01:49

I can not get this code to work on Internet Explorer 11. I know this segment is causing the problem. If I upload my file with this code active, IE 11 dispays entire sections of

相关标签:
2条回答
  • 2021-01-25 02:16

    You should use correct selector:

    For this HTML:

    <div class="ImageContainer">
        <a href="#1234"><img class="selected" src="" /></a>
    </div>
    

    JS:

    $('img').removeClass('selected');
    $('a[href="#1234"]', '.ImageContainer').addClass('selected');
    

    OUTPUT:

    <div class="ImageContainer">
        <a href="#1234" class="selected"><img src="" class=""></a>
    </div>
    

    Tested on IE 11 and FF 42:

    jsfiddle: http://jsfiddle.net/ghorg12110/h1xtty4n/

    0 讨论(0)
  • 2021-01-25 02:30

    The issue is on how IE treats hash. It doesn't default to empty string like the other browsers, it defaults to '#'. Also rather than setting hash, you should set location for reliability in IE.

    hash = window.location.hash !== '' ? window.location.hash: '#about';
    

    becomes

    hash = window.location.hash;
    if (hash !== '' || hash !== '#') {
       hash = '#about';
       window.location = hash;
    }
    

    The markup was not rendering because it was not resetting the hash on entry and giving an error message that '#' was not a valid selector for the filter.

    0 讨论(0)
提交回复
热议问题