最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select an element with an attribute that contains certain text? - Stack Overflow

programmeradmin2浏览0评论

I have this HTML code:

<li class="_33c randomtext3" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;year_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext4" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;group_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext7" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">

The order of the lines is varied, (using [2] doesn't work)

I want to delete the <li> with a certain text (data-gt="...group_ment..."), "group_ment" is the certain text (the line 3, sometimes it is on another line).

I tried with document.querySelectorAll('[data-gt]'); with a NodeList(5), but how to find in that NodeList the certain text?

Thank you.

I have this HTML code:

<li class="_33c randomtext3" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;year_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext4" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;group_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext7" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">
<li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">

The order of the lines is varied, (using [2] doesn't work)

I want to delete the <li> with a certain text (data-gt="...group_ment..."), "group_ment" is the certain text (the line 3, sometimes it is on another line).

I tried with document.querySelectorAll('[data-gt]'); with a NodeList(5), but how to find in that NodeList the certain text?

Thank you.

Share Improve this question edited Dec 17, 2019 at 19:06 Patrick Michaelsen 1,05414 silver badges19 bronze badges asked Dec 17, 2019 at 18:37 Alizee TiconaAlizee Ticona 713 silver badges9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

Use the attribute value selector and wildcard selector.

document.querySelectorAll('li[data-gt*=group_ment]')

See https://www.geeksforgeeks/wildcard-selectors-and-in-css-for-classes/

const matches = document.querySelectorAll('[data-gt*="group_ment"]')
matches.forEach( x => x.classList.add("hidden") )
.hidden {
  display: none;
}
<ul>
  <li class="_33c randomtext3" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}"> pub_ment </li>
  <li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;year_ment&quot;,&quot;from_uids&quot;}"> year_ment </li>
  <li class="_33c randomtext4" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;group_ment&quot;,&quot;from_uids&quot;}"> group_ment </li>
  <li class="_33c randomtext7" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">pub_ment</li>
  <li class="_33c randomtext5" data-gt="{&quot;alert_id&quot;:1576605904117859,&quot;notif_type&quot;:&quot;pub_ment&quot;,&quot;from_uids&quot;}">pub_ment</li>
</ul>

You should use attribute selector bined with the contains modifier *

const mments = document.querySelectorAll('[data-gt*="group_ment"]')

You can see the different modifiers here: Attribute Selectors

发布评论

评论列表(0)

  1. 暂无评论