简体   繁体   English

如何<strong>通过 css</strong>检查列表是否包含第一个元素

[英]how to check if list contain first element as <strong> via css

 <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>

Above HTML is dynamically generated, If <strong> comes first in li (eg 2nd <li> ) then only I need to do some styling.上面 HTML 是动态生成的,如果<strong>li中首先出现(例如第二个<li> ),那么我只需要做一些样式。

Can you please advise if it can be possible via CSS or other alternative?您能否告知是否可以通过 CSS 或其他替代方法实现?

CSS isn't really made for this case, but it would be possible with JavaScript. CSS 并不是真正为这种情况而设计的,但 JavaScript 是可能的。

Really the best thing to do is structure your HTML more semantically, and use a class where you need it.真正最好的办法是更语义化地构建 HTML,并在需要的地方使用 class。

But since you're generating the HTML dynamically and don't have control over the output, here is a solution with JS.但是由于您正在动态生成 HTML 并且无法控制 output,因此这是一个带有 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>

Using javascript you can check the first Node of each <li> and apply a class or inline style to it使用 javascript 您可以检查每个<li>的第一个节点并对其应用 class 或内联样式

Following also accounts for empty text nodes as a result of line breaks in the html以下还说明了由于 html 中的换行导致的空文本节点

 document.querySelectorAll('.container li').forEach(el => { let firstNode = el.childNodes[0]; // account for empty text nodes while (firstNode.nodeType === 3 &&.firstNode.textContent.trim()) { firstNode = firstNode.nextSibling } if (firstNode.nodeName === 'STRONG') { firstNode.classList.add('first-bold') } })
 .first-bold { 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"> <strong>Testing</strong> 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>

ul li:first-child strong {... } will do the trick. ul li:first-child strong {... }会成功的。

Sorry, I misread your question.对不起,我看错了你的问题。 What you want to do isn't really possible in CSS, as far as I know.据我所知,在 CSS 中你想要做的实际上是不可能的。 Best thing to do is have whatever's generating the HTML add a class with the styling you want when it puts a <strong> element at the start of a list item.最好的办法是让任何生成 HTML 的东西添加一个 class ,当它在列表项的开头放置一个<strong>元素时,它具有您想要的样式。

You can apply below style sheet to recognize first child with strong tag:您可以应用以下样式表来识别具有强标签的第一个孩子:

li:first-child > strong {
  background-color: yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM