javascript - 大写块引用的第一个字母时遇到问题
问题描述
$(document).ready(function() {
$("span").wrapInner("<blockquote><q></q></blockquote");
$("blockquote").css("border-top", "1px solid grey");
$("blockquote").css("border-bottom", "1px solid grey");
$("blockquote").css("padding", "5px 0 5px 0");
$("blockquote").css("font-size", "15px");
}); // end ready
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Get going fast</h2>
<p>In just the first 6 chapters of this book, you'll learn more about web development than
you can from most full books.</p>
<p>It's true! In fact, <span>by the end of the crash course in
Section 1, you'll be developing web pages the way today's best professionals do.</span>
That means you'll be using HTML5 semantic elements to mark up the structure of the
content on the page and CSS to format and lay out that content.</p>
我找到了如何将字符串中的第一个字母大写的解决方案,但我认为我没有正确实现它。我收到一条错误消息,提示“blockquote.substr 不是函数”。这是我的jQuery:
$(document).ready(function() {
$("span").wrapInner("<blockquote><q></q></blockquote");
$("blockquote").css("border-top", "1px solid grey");
$("blockquote").css("border-bottom", "1px solid grey");
$("blockquote").css("padding", "5px 0 5px 0");
$("blockquote").css("font-size", "15px");
var blockquote = $("blockquote");
blockquote.substr(0,1).toUpperCase() + blockquote.substr(1);
}); // end ready
解决方案
好吧,您需要使用jQuery中的text()或vanilla javascript中的textContent
例子:
$(document).ready(function() {
const blockquote = $("blockquote"); // yep, i like using this
const text = blockquote.text()
.trim(); // do u need new lines? nope...
blockquote.css({ // u can using object to pass all css styles
"border-top": "1px solid grey",
"border-bottom": "1px solid grey",
"padding": "5px 0 5px 0",
"font-size": "15px"
});
blockquote.text(text.charAt(0).toUpperCase() + text.substr(1));
}); // end ready
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<blockquote>
hello wolrd
</blockquote>
</body>
</html>
如果您想将所有单词大写:
// create a function to capitalize some word
const capitalize = value => value.charAt(0).toUpperCase() + value.substr(1);
// split into array based on spaces, and capitalize all words to finally join to make a full capitalized string
blockquote.text(text.split(' ').map(i => capitalize(i)).join(' '))
如果您有多个带有 te 选择器的项目,会发生什么?你可以each
这样使用:
$(document).ready(function() {
const blockquote = $("blockquote"); // yep, i like using this
blockquote.each((index, element) => {
const text = $(element).text()
.trim(); // do u need new lines? nope...
$(element).css({ // u can using object to pass all css styles
"border-top": "1px solid grey",
"border-bottom": "1px solid grey",
"padding": "5px 0 5px 0",
"font-size": "15px"
});
$(element).text(text.charAt(0).toUpperCase() + text.substr(1));
})
}); // end ready
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<blockquote>
hello wolrd
</blockquote>
<blockquote>
wow
</blockquote>
</body>
</html>
提示#1:
在 jQuery 中,您可以
element.text()
使用element.text('some text')
.提示#2:
在 jQuery 中,您可以使用
element.css('prop', 'value')
一种样式,但如果您有多个样式,则可以尝试element.css({ ... })
使其更简单明了..提示 #3
在 javascript 中,一个字符串是一个字符数组,所以你 cat 选择索引为
text[0]
or的第一个元素text.charAt(0)
。提示 #4
如果您使用 html 并且文本有新行(
\n
),您可能想要删除它...请使用修剪或正则表达式/\n+/g
删除它提示 #5
在 javascript 中,您可以使用 split 基于特定字符拆分字符串。它将所有项目分开并返回一个字符串数组,因此更容易迭代。
提示 #6
在 jQuery 中,你可以使用 CSS 选择器来选择一些元素:
#my-element
- 选择具有特定 ID 的元素blockquote:nth-child(0)
- 根据元素在一组兄弟中的位置匹配元素blockquote + blockquote
- 一个又一个的块引用blockquote
blockquote:first-child
- 第一个元素...
推荐阅读
- r - parse_date_time 返回一个 NA 向量
- c# - 尝试在 SQL 字符串中使用 GUID 时出现语法错误
- bash - 在 WSL bash 脚本中将目录更改为 Window 的用户目录
- node.js - 如何用 Homebrew 的节点替换我的节点版本?
- c++ - 如何对对的向量进行排序
按第一个和第二个值? - python - 正则表达式匹配包含月份的日期范围
- sql - 获取两列,一列包含分组值,另一列不包含分组值
- javascript - 切换 TAB 时未调用 statusChanges
- python - Pandas 中的箱线图:带聚合的分组箱线图
- batch-file - 如何根据每个文件名中的文件夹名称将文件移动到现有文件夹?