[英]How to select an element with an attribute that contains certain text?
我有这个 HTML 代码:
<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"}">
行的顺序是不同的,(使用 [2] 不起作用)
我想删除带有特定文本( data-gt="...group_comment..."
)的<li>
, "group_comment"
是特定文本(第 3 行,有时在另一行)。
我试过document.querySelectorAll('[data-gt]');
使用NodeList(5)
,但是如何在该NodeList
找到特定文本?
谢谢你。
使用属性值选择器和通配符选择器。
document.querySelectorAll('li[data-gt*=group_comment]')
见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>
您应该结合使用属性选择器和 contains 修饰符*
const commments = document.querySelectorAll('[data-gt*="group_comment"]')
您可以在此处查看不同的修饰符: 属性选择器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.