【问题标题】:Style first letter of each word in paragraph段落中每个单词的样式首字母
【发布时间】:2017-05-04 07:04:29
【问题描述】:

我正在尝试使用CSS 为段落的第一个字母设置样式,并想使用 greensock 添加一些动画,但实际上要求是为每个单词的第一个字母设置样式,而不仅仅是第一个字母段落。

对此有何建议/想法?

p{
  font-size:150%;
  color:#000000;
}
p::first-letter {
  font-size: 200%;
  color: #ff0000;
}
<p>Hello This Is The Title</p>

更新我尝试了以下方式处理(添加跨度标签并定位每个跨度的第一个元素)但它不起作用:

p span:nth-child(1)::first-letter {
   font-size: 200%;
   color: #ff0000;
}

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

与split(" ") 一起使用创建数组形式的字符串,forEach() 用于迭代每个单词。然后slice(0,1)剪切单词的第一个字母,然后附加span。并添加带有span的css效果

var str = $('p').text().split(" ");
$('p').empty();
str.forEach(function(a) {
  $('p').append('&nbsp;<span>' + a.slice(0, 1) + '</span>' + a.slice(1))
})
p {
  font-size: 150%;
  color: #000000;
}

span {
  font-size: 200%;
  color: #ff0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello This Is The Title</p>

【讨论】:

    【解决方案2】:

    没有 css 首词 css 选择器。所以你可以使用 jquery 来实现。

    解决方案 1: 仅设置段落的第一个单词的样式。

    $(function() {
        $('p').each(function() {
            var text = this.innerHTML;
            var firstSpaceIndex = text.indexOf(" ");
            if (firstSpaceIndex > 0) {
                var substrBefore = text.substring(0,firstSpaceIndex);
                var substrAfter = text.substring(firstSpaceIndex, text.length)
                var newText = '<span class="firstWord">' + substrBefore + '</span>' + substrAfter;
                this.innerHTML = newText;
            } else {
                this.innerHTML = '<span class="firstWord">' + text + '</span>';
            }
        });
    });
    .firstWord{ color:red; font-size:20px;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>Styling the first word of pragraph.</p>

    解决方案 2: 段落行的每个首字母都设置样式

    $(document).ready(function() {
        var words = $('p').text().split(' ');
        var html = '';
        $.each(words, function() {
            html += '<span class="firstLetter">'+this.substring(0,1)+'</span>'+this.substring(1) + ' ';
        });
        $('p').html(html);
    });
    .firstLetter{ color:red; font-size:20px;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>Styling each  first letter of the word in  pragraph.</p>

    【讨论】:

      【解决方案3】:

      const p = document.getElementById('text')
      
      const styleMe = l => '<span class="styled">' + l + '</span>'
      
      const newS = p.innerText.split(' ').map(w => w.split('').map((l,i) => (i === 0) ? styleMe(l) : l).join('')).join(' ')
      
      p.innerHTML = newS
      .styled {
        color:red
      }
      &lt;p id="text"&gt;Hello This Is The Title&lt;/p&gt;

      【讨论】:

        【解决方案4】:

        您正在寻找的是一个不存在的伪元素。有 :first-letter 和 :first-line,但没有 :first-letter-every-word。

        最简单的选择是将每个单词的第一个字母包裹在&lt;span&gt; 中。另一种选择是尝试使用 javascript 解决方案。

        【讨论】:

        • 这是评论(不是答案)。
        猜你喜欢
        • 2020-11-16
        • 1970-01-01
        • 2020-08-30
        • 2017-08-02
        • 2013-12-15
        • 1970-01-01
        • 2016-12-05
        • 2015-11-10
        • 1970-01-01
        相关资源
        最近更新 更多