Как найти div, который содержит слово с точным соответствием

Я проверил следующую ссылку:

jQuery :contains(), но для точного соответствия строке

Но как только я найду div, мне нужно установить атрибут:

 $('#viewer .textLayer div:contains('+hotspot_draw_join+')').each(function () {
            $(this).attr('class','secondtypehotspot secondtypehotspot_ref'); 
            $(this).attr('id',hotspotstr);
            $(this).attr('onclick','secondtypehotspotfn(event)');


           });

Как мне это сделать, используя опцию фильтра


person Vishnu    schedule 26.09.2018    source источник
comment
api.jquery.com/filter   -  person Ason    schedule 26.09.2018


Ответы (1)


Вы можете попробовать так. Это работает нормально.

 <div id="id">
     <p>John</p>
     <p>Johny</p>
 </div>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script>
     var results = $('#id').find('*').filter(function () {
         if ($(this).text() === 'John') {
             $(this).attr("data-val", "hello");
             return $(this).text() === 'John';
         }
     });
     results.css('color', 'red');
 </script>
person Hardik Karangia    schedule 26.09.2018