Jquery select first letter?

后端 未结 5 1214
时光说笑
时光说笑 2020-12-31 05:24

I am simply attempting to get jquery to identify the first letter of a paragraph. How would I do this?

For example, I have a page with a number of paragrahs on a pa

5条回答
  •  醉话见心
    2020-12-31 05:47

    If you'd like to use JavaScript to grab the letter from the URL query string, run a regular expression on window.location.search:

    var letterParam = window.location.search.match(/letter=([a-z])/i), letter;
    
    if (letterParam)
    {
        letter = letterParam[1];
    }
    

    To match paragraphs starting with that letter, use the charAt() method in JavaScript strings:

    if (letter)
    {
        $('p').each(function()
        {
            if ($(this).text().charAt(0).toUpperCase() == 'B')
            {
                // Apply the CSS class or change the style...
            }
        });
    }
    

提交回复
热议问题