简体   繁体   English

Javascript/Jquery - 删除所有文本内容 - 除了最后 10 个字符(或 3 个单词) - 元素之前

[英]Javascript/Jquery - Remove all text content - except for the last 10 characters (or 3 words) - before an element

I would like to remove all content inside the <p class="content"> paragraph before the element, EXCEPT for the last 10 characters, and ideally add ... before the first character:我想删除元素之前<p class="content">段落中的所有内容,除了最后 10 个字符,最好在第一个字符之前添加 ...:

I have this:我有这个:

<p class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p>

And I would like to end up with something like this (it can also by by word - 2 or 3 words before the <span class="highlight">我想以这样的方式结束(它也可以按字 - 在<span class="highlight">之前的 2 或 3 个字)

<p class="content">...med doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p>

You can use split to identify the highlight tag and add it before remove the letters.您可以使用split来识别突出显示标记并在删除字母之前添加它。 Create a var with total letter to rest after ... ;创建一个带有总字母的 var 以在...之后休息;

Check this example:检查这个例子:

 var el = document.querySelector('.content'); var amountLetterBeforeHighlight = 25; var splitEl = el.innerHTML.split('<span class="highlight">') el.innerHTML = '... :rest<span class="highlight">:splitRest '.replace(/\\:rest/g, splitEl[0].substr(splitEl[0].length - amountLetterBeforeHighlight)).replace(/\\:splitRest/g, splitEl[1])
 .highlight { color: blue }
 <p class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p>

EDIT:编辑:

So you want to create with multiples paragraphs, so check this out:所以你想用多个段落创建,所以看看这个:

 var els = document.querySelectorAll('.content'); var amountLetterBeforeHighlight = 25; els.forEach(el => { var splitEl = el.innerHTML.split('<span class="highlight">'); el.innerHTML = '... :rest<span class="highlight">:splitRest '.replace(/\\:rest/g, splitEl[0].substr(splitEl[0].length - amountLetterBeforeHighlight)).replace(/\\:splitRest/g, splitEl[1]); });
 .highlight {color: blue}
 <p class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p> <p class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p> <p class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied. </p>

No matter what is inside, after the code find the highlight tag then apply to each el at time.不管里面是什么,在代码找到highlight标签之后,再一次应用到每个el

Hope this help!希望这有帮助!

See the example below, comments within code.请参阅下面的示例,代码中的注释。

 //get innerHTML of p var p = document.getElementById("p").innerHTML; //check length of p - uncomment below to see length //console.log(p.length) //length of p was 611 so we just want to get index 605 - 611 of p var pslice = p.slice(605,611); //declare new p for example by id "shortp" var shortp = document.getElementById("shortp") //set innerHTML of example "shortp" with ... + pslice shortp.innerHTML = "..." + pslice
 <p id="p" class="content">It sportsman earnestly ye preserved an on. Moment led family sooner cannot her window pulled any. Or raillery if improved landlord to speaking hastened differed he. Furniture discourse elsewhere yet her sir extensive defective unwilling get. Why resolution one motionless you him thoroughly. Noise is round to in it quick timed doors. <span class="highlight">loving</span> family. Born in Brewer, ME on August 21, 1934, she was the daughter of Able. Written address greatly get attacks inhabit pursuit our but. Lasted hunted enough an up seeing in lively letter. Had judgment out opinions property the supplied.</p> <p><i>Your shortened example below</i></p> <p id="shortp"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除JavaScript中除last以外的特定单词? - Remove specific words except last in JavaScript? jQuery / JavaScript:如何删除第一个中的所有元素 <li> <a>文字</a>除外 - jQuery/JavaScript: how to remove all elements in the first <li> except <a>text</a> jQuery删除除第一个和最后一个数字外的所有数字 - jQuery remove all except first and last numbers 使用javascript删除除字母数字和空格之外的所有字符 - Remove all characters except alphanumeric and spaces with javascript 使用javascript / jquery删除&#39;/&#39;字符的最后一次迭代之前的所有文本 - Remove any text before the last iteration of a '/' character with javascript / jquery Javascript / jQuery从字符串中删除所有字符,但特定字符之间的出现除外 - Javascript/jQuery remove all characters from string except occurrences between specific characters 如何删除除javascript中的一个以外的所有文本? - how to remove all text except one in javascript? jQuery删除字符串中所有空格,除非单词之间不起作用 - Jquery remove all spaces in string except between words not working 如何删除jQuery中第一个元素和第二个元素以外的所有元素? - How to remove all element Except first element and Second element in jQuery? Javascript正则表达式-删除字符串中除单词之间的所有空格 - Javascript regex- remove all spaces in string except between words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM