I have this HTML code:
<li class="_33c randomtext3" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}">
<li class="_33c randomtext5" data-gt="{"alert_id":1576605904117859,"notif_type":"year_comment","from_uids"}">
<li class="_33c randomtext4" data-gt="{"alert_id":1576605904117859,"notif_type":"group_comment","from_uids"}">
<li class="_33c randomtext7" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}">
<li class="_33c randomtext5" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}">
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_comment..."
), "group_comment"
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.
Use the attribute value selector and wildcard selector.
document.querySelectorAll('li[data-gt*=group_comment]')
See https://www.geeksforgeeks.org/wildcard-selectors-and-in-css-for-classes/
const matches = document.querySelectorAll('[data-gt*="group_comment"]') matches.forEach( x => x.classList.add("hidden") )
.hidden { display: none; }
<ul> <li class="_33c randomtext3" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}"> pub_comment </li> <li class="_33c randomtext5" data-gt="{"alert_id":1576605904117859,"notif_type":"year_comment","from_uids"}"> year_comment </li> <li class="_33c randomtext4" data-gt="{"alert_id":1576605904117859,"notif_type":"group_comment","from_uids"}"> group_comment </li> <li class="_33c randomtext7" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}">pub_comment</li> <li class="_33c randomtext5" data-gt="{"alert_id":1576605904117859,"notif_type":"pub_comment","from_uids"}">pub_comment</li> </ul>
You should use attribute selector combined with the contains modifier *
const commments = document.querySelectorAll('[data-gt*="group_comment"]')
You can see the different modifiers here: Attribute Selectors
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.