jQuery selector where text = some value

后端 未结 9 1122
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 17:12

I have an object (in this case a rating object from js-kit) that I want to make invisible if the rating value is \'unrated\'. I\'m having trouble with getting the right jQue

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 17:40

    A slight variant on @tgmdbm's excellent answer. The only difference being that it only selects nodes that have a single child text node exactly matching the hasText() argument. Whereas .innerText returns the concatenation of all descendant text nodes.

      if( ! $.expr[':']['hasText'] ) {
         $.expr[':']['hasText'] = function( node, index, props ) {
           var retVal = false;
           // Verify single text child node with matching text
           if( node.nodeType == 1 && node.childNodes.length == 1 ) {
             var childNode = node.childNodes[0];
             retVal = childNode.nodeType == 3 && childNode.nodeValue === props[3];
           }
           return retVal;
         };
      }
    

提交回复
热议问题