javascript - 如何通过css检查列表是否包含第一个元素
问题描述
解决方案
CSS 并不是真正为这种情况而设计的,但它可以使用 JavaScript。
最好的办法是更加语义化地构建 HTML,并在需要的地方使用类。
但是由于您是动态生成 HTML 并且无法控制输出,因此这里有一个 JS 解决方案。
document.querySelectorAll('ul li').forEach(element => {
if (element.innerHTML.startsWith('<strong>') ) {
element.querySelector('strong').style = 'color:red'
}
})
<div class="container">
<ul>
<li class="info">Testing Bold text in custom bullets</li>
<li class="info"><strong>Testing</strong> Bold text in custom bullets</li>
<li class="info">Testing <strong>Bold</strong> text in custom bullets</li>
<li class="info">Testing Bold <strong>text</strong> in custom bullets</li>
<li class="info">Testing Bold text <strong>in</strong> custom bullets</li>
<li class="info">Testing Bold text in <strong>custom</strong> bullets</li>
<li class="info">Testing Bold text in custom <strong>bullets</strong></li>
<li class="info">Testing Bold text in custom bullets</li>
</ul>
</div>
推荐阅读
- excel - Visual Basic 查询 (excel)
- c# - 从数据库获取数据作为日期时间时出现 InvalidCastException
- testing - 通过在 TestCafe 中包含文本来选择元素
- hp-nonstop - TACL 如何使用多个参数
- python - 将nan转换为数字
- java - 如何使用带有命名属性和声明为附加属性的属性的 openAPI 模型的 swagger-codegen
- javascript - Fabric Js无法设置值originY底部
- python - OSError: [WinError 126] 在 Python 中找不到指定的模块
- proxy - WSDL.exe - 如何生成 cs 文件
- google-cloud-platform - 如何一次性将整个 blob(图像)文件夹从谷歌存储桶下载到 AI Platform Notebooks?