使用CSS和jQuery将所有CAPS转换为正确的大小写 [英] Transform ALL CAPS to Proper Case using CSS and jQuery

查看:93
本文介绍了使用CSS和jQuery将所有CAPS转换为正确的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试类似此主题的操作 http://stackoverflow.com/a/3471258/2117845
,我想我想要的东西像Harmen发布的jquery代码,并说它现在是一个插件?

I'm trying to do something similar to this thread http://stackoverflow.com/a/3471258/2117845 and I think I want something like Harmen posted with the jquery code and said it's now a plugin?

我有的是所有的CAPS标题来自数据库。例如:

What I have is ALL CAPS titles coming from a database. For example:

快乐的棕色狐狸是关于弗吉尼亚州的美景

THE QUICK BROWN FOX IS ABOUT TO JUMP OVER THE FENCE

我想看起来像:

Quick Brown Fox即将跳过围栏

The Quick Brown Fox is About to Jump Over the Fence

CSS和jQuery来转换文本和消除某些单词被正确的情况下?但我想我需要保持第一次。就像我删除了the,is,to,and,那么标题中的第一个the将全部是小写的。所以,我能够以某种方式消除这些常见的词,只有不是第一个词?

So is there a way to do this with CSS and jQuery to transform the text and eliminate certain words from being made into Proper Case? But I guess I'd need to keep the first instance. Like if I eliminated "the, is, to, and" then the first "the" in the title would be all lowercase. So would I be able to somehow eliminate those common words only if it's not the first word?

我知道这是很多,但我不是非常熟悉jQuery。有任何地方的教程或插件,显示如何做这样的事情。我已经看到许多关于如何转换所有CAPS文本的问题,所以解决方案可能会非常有用。

I know that's a lot but I'm not super familiar with jQuery. Is there a tutorial or plugin anywhere that shows how to do something like this. I've seen many questions about how to transform ALL CAPS text so a solution would probably be quite useful.

推荐答案

代码 Greg Dean在相关问题中的回答。

我将这个算法称为最有效的猜测,因为它实际上不评估英语大写规则。你可能需要在noCap列表中添加更多的单词以获得更好的准确性。

I would call this algorithm an educated guess at best since it's not actually evaluating English capitalization rules. You'll probably need to add more words to the noCaps list for better accuracy.

JS

var ele = $('p'); 
ele.text(toProperCase(ele.text())); 

function toProperCase(str)
{
    var noCaps = ['of','a','the','and','an','am','or','nor','but','is','if','then', 
'else','when','at','from','by','on','off','for','in','out','to','into','with'];
    return str.replace(/\w\S*/g, function(txt, offset){
        if(offset != 0 && noCaps.indexOf(txt.toLowerCase()) != -1){
            return txt.toLowerCase();    
        }
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}

HTML

<p>THE QUICK BROWN FOX IS ABOUT TO JUMP OVER THE FENCE</p>

这篇关于使用CSS和jQuery将所有CAPS转换为正确的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆