Getting all links with specific inner HTML value in jQuery

后端 未结 4 1646
时光取名叫无心
时光取名叫无心 2021-02-05 04:56

I

相关标签:
4条回答
  • 2021-02-05 05:28

    You ask why this doesn't work:

    $('div').find('a').find(':contains("Text2")')
    

    The reason is, .find() will search children elements, you want .filter() (because you already selected the a - or you add the :contains to the a find:

    $('div').find('a').filter(':contains("Text2")');
    $('div').find('a:contains("Text2")');
    
    0 讨论(0)
  • 2021-02-05 05:41

    You're looking for contains:

    $("a:contains('text2')")
    
    0 讨论(0)
  • 2021-02-05 05:53

    As an additional note, rather than scanning for exact text inside a link it might be better to be scanning for attributes, e.g.

    <div class="farm-market-items">
      <a class="market-item" data-item-type="seed" data-item-id="817">
        Carrot Seed
        <img alt="" src="" class="item-thumb" />
      </a>
      <a class="market-item" data-item-type="seed" data-item-id="25">
        Spinach Seed
      </a>
      <a class="market-item" data-item-type="tree" data-item-id="981">
        Pear Tree
      </a>
    </div>
    

    Now you can (accurately) scan for:

    all_seeds = $('a[data-item-type="seed"]');
    

    (I'm a big fan of the data-* attributes.)

    0 讨论(0)
  • 2021-02-05 05:54

    Use the :contains() filter.

    http://api.jquery.com/contains-selector/

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