如何检测第一个字母是否不包含连字符



我试图检测:如果第一个字母不是连字符,则在前面加上元素。这是我的尝试:

$('.post-category-label .item:first-letter(:not(:contains("-")))').insertBefore('<span class="item">Uncategorized</span>');

但是,Chrome控制台表示jQuery不支持:first-letter

我该如何完成这项工作?

编辑:将.prepend()更改为insertBefore()

您可以过滤掉包含连字符作为第一个字母的选择器,而不是使用 JQuery 选择器。

$('.post-category-label .item').filter((index, item) => item.innerHTML.split('')[0] !== '-').prepend('<span class="item">Uncategorized </span>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="post-category-label">
<span class="item">- test</span><br>
<span class="item">other test</span><br>
<span class="item">third test</span><br>
<span class="item">- final test</span><br>
</span>

最新更新