简体   繁体   English

用javascript追加问题改变每个单词的第一个字母的字体大小

[英]changing font size of first letter of each word with javascript append issue

I have this js that is adjusting the size of the first letter of each word in my headers. 我有这个js调整我的标题中每个单词的第一个字母的大小。 The only prob is that it appends all the results to each header. 唯一的问题是它将所有结果附加到每个标头。 So each header on the page, becomes a combo of every header on the page. 因此页面上的每个标题都成为页面上每个标题的组合。 This jsfiddle link makes what Im saying much more clear: 这个jsfiddle链接让我说得更清楚:

http://jsfiddle.net/GKYuG/ http://jsfiddle.net/GKYuG/

Can someone please let me know how I can get the correct result? 有人可以告诉我如何才能得到正确的结果吗?

Try this: 尝试这个:

$(document).ready(function() { 

    var titles = new Array();
    titles[0] = 'latest_news';
    titles[1] = 'contact_me';

    $('.title').each(function(){
        var words = $(this).text().split(' '); 
        var html = ''; 
        $.each(words, function() { 
            html += '<span style="font-size:200%">'+this.substring(0,1)+'</span>'+this.substring(1) + ' '; 
        }); 
        $(this).html(html); 
    });

}); 

You have to iterate through the selected divs, otherwise you are applying your changes to all the divs selected by the selector. 您必须遍历所选的div,否则您将更改应用于选择器选择的所有div。

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

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