Count Paragraph Text Length with jQuery

preview_player
Показать описание
This short demo shows how to count paragraph text length with jQuery, and also append the number in the end of each paragraph.
Рекомендации по теме
Комментарии
Автор

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
span.num {
color: firebrick;
}
</style>
</head>

<body>
<div class="container">
<div class="articles">
<h1>Count Paragraph Length</h1>
<ul class="article-list">
<li class="article-item featured">
<header>Article #2</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil animi ipsum, incidunt mollitia modi cum, eum ex doloremque rerum quod, maiores quisquam, enim quam unde cumque! Quam, doloremque. Voluptatum, maxime!</p>
<li class="article-item">
<header>Article #3</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt ratione, cum doloremque molestias labore amet at dicta reiciendis repudiandae fuga atque adipisci pariatur distinctio suscipit perferendis provident, facere ad aut.</p>
</li>
<li class="article-item">
<header>Article #4</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate commodi, est soluta eos voluptates eum! Modi sit quia ipsum fugiat nesciunt, assumenda deleniti consequuntur molestias reprehenderit. Voluptate corporis illum nam?</p>
<p>Quia, omnis. Quasi necessitatibus, ducimus, deserunt ipsa, reprehenderit sit a dolore fuga placeat magni culpa ipsam quas id aut veniam rerum pariatur enim harum recusandae quo odio consequuntur. Maxime, aperiam.</p>
</li>
</ul>
</div>
</div>
<script>
$("p").each(function() {
var num = $(this).text().length;
$(this).append('<span class="num"> Number of This Paragraph: </span>' + num);
});
</script>

</body>
</html>

ChuweiLu