Why can't jQuery 3 identify the '#' character in an attribute selector?

后端 未结 2 947
说谎
说谎 2020-12-04 02:45

I just tried switching my application to jQuery 3. I was going through some testing and everything was working as expected, until I came to a piece of my application that us

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 02:54

    Note, the change apparently took place at version 2.0, as version 2.1.3 returned element using selector

    var $existingFilter1 = $container.find('.filterFeedItem[data-component-type=#somefilter]');
    

    jsfiddle https://jsfiddle.net/f8nej922/2/

    Though have not been able to locate specific reference to or description of change at jQuery 2.2 and 1.12 Released documentation.

    As noted by @BoltClock, change is related to Selector: Remove "#" exception for identifier tokens.


    You can esacape # character with \\; quote value at attribute selector; or use $.escapeSelector()

    var $existingFilter = $container
                          .find('.filterFeedItem[data-component-type=\\#somefilter]');
    

    var $existingFilter = $container
                          .find('.filterFeedItem[data-component-type="#somefilter"]');
    

    var $existingFilter = $container
                          .find('.filterFeedItem[data-component-type=' 
                           + $.escapeSelector('#somefilter') + ']');
    

    jsfiddle https://jsfiddle.net/f8nej922/4/

提交回复
热议问题