jQuery selector where text = some value

后端 未结 9 1118
爱一瞬间的悲伤
爱一瞬间的悲伤 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:41

    Perhaps the issues is with the :contains('Unrated') part of the function. As changing the text to any random value produces the same result:

    $("#ratingDiv:contains('somerandomtext')").hide();
    
    0 讨论(0)
  • 2020-12-03 17:42

    You can create your own selector methods

    For example, if you want to be able to do the following:

    $('.js-rating-label:hasText(unrated)');
    

    You can define the hasText method as follows

    $.expr[':']['hasText'] = function(node, index, props){
      return node.innerText.contains(props[3]);
    }
    

    props[3] contains the text inside the brackets after ':hasText'.

    0 讨论(0)
  • 2020-12-03 17:52

    You might consider using the hide() / show() methods as well.

    $(".js-rating-labelText:contains('unrated')").hide()
    
    0 讨论(0)
提交回复
热议问题