CSS to select/style first word

后端 未结 11 1845
礼貌的吻别
礼貌的吻别 2020-11-22 13:30

This one has me kind of stumped. I want to make the first word of all the paragraphs in my #content div at 14pt instead of the default for the paragraphs (12pt). Is there a

11条回答
  •  余生分开走
    2020-11-22 14:01

    Here's a bit of JavaScript and jQuery I threw together to wrap the first word of each paragraph with a tag.

    $(function() {
        $('#content 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 = '' + substrBefore + '' + substrAfter;
                this.innerHTML = newText;
            } else {
                this.innerHTML = '' + text + '';
            }
        });
    });
    

    You can then use CSS to create a style for .firstWord.

    It's not perfect, as it doesn't account for every type of whitespace; however, I'm sure it could accomplish what you're after with a few tweaks.

    Keep in mind that this code will only execute after page load, so it may take a split second to see the effect.

提交回复
热议问题