是否有相当于 CSS text-transform: Capitalize 的 JS?
我有一个隐藏的 ,它由 div 组成,其中包含要填充到 jQuery UI 对话框中的内容。在 document.ready 上,我想循环遍历这些 div,获取每个 div 的 id,用空格替换破折号,将每个单词大写,并将其存储在标题变量中。然后,我将在放入我的
dialogs[]
数组的对象文字中使用它。听起来很简单,对吧?
HTML 的精简版:
<section id="dialog-content" class="hidden">
<div id="some-dialog">
// awesome dialog content here
</div>
<div id="another-dialog">
// awesome dialog content here
</div>
<div id="modal-dialog">
// awesome dialog content here
</div>
</section>
JavaScript 的精简版:
var dialogs = [],
$container = $("#dialog-content");
$content = $container.find("> div");
$content.each(function (i)
{
var $this = $(this),
id = $this.attr("id"),
title = id.replace(/\-/g, " ");
console.log(title);
dialogs[dialogs.length] =
{
trigger: $("#" + id + "-trigger"),
title: title,
content: $this.html()
};
});
顺便说一句 - 我知道我可以使用 $.data()
并向我的 div 添加自定义属性,但我真的想要尽可能少的标记,并且我对这种特定的可能性感到好奇。所以这不是我的例子,而是手头的问题。
重申一下,问题是:
如何通过 JavaScript 将变量中的每个单词大写,只需就像 CSS 中的 text-transform: Capitalize;
一样?
I've got a hidden <section />
that is comprised of divs that contain content to be stuffed into a jQuery UI dialog. On document.ready I want to loop through those divs, take the id of each respective div, replace dashes with spaces, capitalize each word, and store that in a title variable. Then, I'm going use that in an object literal that gets put into my dialogs[]
array. Sounds simple, right?
Stripped down version of the HTML:
<section id="dialog-content" class="hidden">
<div id="some-dialog">
// awesome dialog content here
</div>
<div id="another-dialog">
// awesome dialog content here
</div>
<div id="modal-dialog">
// awesome dialog content here
</div>
</section>
Stripped down version of the JavaScript:
var dialogs = [],
$container = $("#dialog-content");
$content = $container.find("> div");
$content.each(function (i)
{
var $this = $(this),
id = $this.attr("id"),
title = id.replace(/\-/g, " ");
console.log(title);
dialogs[dialogs.length] =
{
trigger: $("#" + id + "-trigger"),
title: title,
content: $this.html()
};
});
BTW - I know I can use $.data()
and add custom properties to my divs, but I really wanted as minimal of markup as possible, and I am curious about this specific possibility. So it's not so much my example, but the question at hand.
To re-iterate, the question is:
How can I capitalize each word in a variable via JavaScript, just like text-transform: capitalize;
does in CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地编写一个函数来完成它。这是一个简单的基于正则表达式的函数:
You could simply write a function to do it. Here is a simple regex-based function:
这可能会有所帮助: Javascript:字符串中每个单词的大写。
编辑以包含上述链接答案中的内容:
以下内容将处理所有 Unicode 字母,这与使用
\w
、的任何解决方案不同\b
或[az]
等。不可否认,正则表达式相当大。字符列表取自 Steve Levithan 优秀的 XRegExp 库。This might be of help: Javascript: Capitalization of each Word in String.
Edit to include the content from the aforementioned linked answer:
The following will handle all Unicode letters, unlike any solution using
\w
,\b
or[a-z]
etc. Admittedly the regular expression is rather large. Character lists taken from Steve Levithan's excellent XRegExp library.感谢 @MarkCostello 和他发现的问题,我将其中的 2 个答案合并为创建我自己的终极
capitalize()
实用程序:Thanks to @MarkCostello and the question he found, I've combined 2 answers from there to create my own ultimate
capitalize()
utility: