我需要帮助。
我希望将特定div中的文本的前n个(例如200个)字符放在 span 中,并用CSS进行修饰,而201字符则更正常,不在范围内
现在,我有了这个生成的HTML:
<div class="container">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
</div>
我需要这个;
<div class="container">
<p><span class="big">Lorem Ipsum is simply dummy text of the printing and typesetting</span> industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
</div>
我找到了这个http://jsfiddle.net/UX3sC/,但是代码复制了txt内容,我不需要它
var content = $('.content');
content.prepend('<span class="entry-content">' + content.text().substring(0, 200) + '</span><br/><br/>');
.entry-content {
font-size: x-large
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Header</h1>
<span class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque metus est, elementum ut sollicitudin quis, posuere at nisl. Duis venenatis sodales eleifend. Ut pharetra turpis at tortor mattis nec tempus est suscipit. Mauris luctus ante quis justo ornare ornare. Nulla tellus neque, pretium quis pellentesque in, egestas eget erat. Integer id erat semper lectus mollis volutpat. Ut imperdiet massa eget leo pharetra at venenatis turpis posuere. Nullam tempus magna metus, eu ultricies orci. Morbi vulputate aliquet enim vitae bibendum. Duis rutrum mi consequat nisi tempus vehicula. Proin nulla nisi, laoreet et consectetur vitae, scelerisque a justo. Vivamus enim justo, adipiscing at scelerisque at, pulvinar suscipit sem. </span>
答案 0 :(得分:0)
假设p的html中没有其他标签,我们可以这样做
var $p = $(".container p");
$p.html(function(_,html) {
return `<span class="big">${html.substring(0,200)}</span>${html.substring(200)}`
})
.big { font-size:x-large }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque metus est, elementum ut sollicitudin quis, posuere at nisl. Duis venenatis sodales eleifend. Ut pharetra turpis at tortor mattis nec tempus est suscipit. Mauris luctus ante quis justo ornare ornare. Nulla tellus neque, pretium quis pellentesque in, egestas eget erat. Integer id erat semper lectus mollis volutpat. Ut imperdiet massa eget leo pharetra at venenatis turpis posuere. Nullam tempus magna metus, eu ultricies orci. Morbi vulputate aliquet enim vitae bibendum. Duis rutrum mi consequat nisi tempus vehicula. Proin nulla nisi, laoreet et consectetur vitae, scelerisque a justo. Vivamus enim justo, adipiscing at scelerisque at, pulvinar suscipit sem. </p>
</div>