Safari bug :first-child doesn't update display:block when items are removed with JS

后端 未结 1 1806
名媛妹妹
名媛妹妹 2020-12-10 21:38

With a list of items where all are hidden by default, the first li has a display of block. The problem is that this won\'t update if t

相关标签:
1条回答
  • 2020-12-10 22:22

    This appears to be a problem with display: none and objects removed from the document tree which manifests itself when you use :first-child, rather than a problem intrinsic to Safari's handling of the :first-child selector itself.

    Either way, this is definitely a bug. jQuery doesn't destroy the object even when you detach it (and its contents) from its parent, but when detaching an element from its parent it should no longer be the nth child of its parent for whatever value of n, so the next element that becomes the first child should match :first-child accordingly.

    If you change :first-child in your code to :not(:last-child), like this, such that you have two elements displaying at a time, you'll notice in Safari when you click the button the first element disappears, leaving the second element intact (as well as the third which is still hidden).

    I also found that if you add a new empty rule with the :empty selector on the list itself:

    /* Or even .list:empty even though it's not actually empty */
    .list:not(:empty) {}
    

    Everything will suddenly work correctly in Safari. Even more bizarre is that this workaround does not work with any other level 3 pseudo-class. It only works with :empty or :not(:empty).

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