jQuery: find element by text

后端 未结 7 2210
攒了一身酷
攒了一身酷 2020-11-22 03:31

Can anyone tell me if it\'s possible to find an element based on its content rather than by an id or class?

I am attempting to find

7条回答
  •  花落未央
    2020-11-22 04:15

    Fellas, I know this is old but hey I've this solution which I think works better than all. First and foremost overcomes the Case Sensitivity that the jquery :contains() is shipped with:

    var text = "text";
    
    var search = $( "ul li label" ).filter( function ()
    {
        return $( this ).text().toLowerCase().indexOf( text.toLowerCase() ) >= 0;
    }).first(); // Returns the first element that matches the text. You can return the last one with .last()
    

    Hope someone in the near future finds it helpful.

提交回复
热议问题