简体   繁体   中英

In jQuery, how to find all elements matching given strings?

I'm trying to find out how to select all <span class="title"> elements on the page:

<div class="my-page">
    <span class="title">first</span>
    <span class="title">second</span>
    <span class="title">third</span>
    <span class="title">fourth</span>
    <span class="title">fifth</span>
</div>

that are present on the list (fetched from servlet in JSON format) and stored inside some variable?

I've tried something like:

success : function(data, statusText, jqxhr) {
  var arr = data.selectedTitles;
    jQuery.each(data.favTools, function(i, v) {
      var title = v;
      jQuery('span.title:contains(title)').css("text-decoration", "underline");
    });
}
$('span.title').filter(function() {
    return $(this).text().indexOf(title) != -1;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM