当句子超过某个单词时,在某处将其截断

发布于 2025-01-19 00:10:25 字数 989 浏览 3 评论 0原文

if 函数时它就失效了

我想要某个句子在超过一定字符限制时被打断,是的,我使用了 substr 但是当我使用JS 的

$(function () {
  $.getJSON("/admin/getposts", function (data) {
    $(".postsMain").empty();
    $.each(data.reverse(), function (v, veri) {
    let item = veri.data
    let content = item.content
    if(content > 70) {
    const generalContent = content.set((content).substr(0,110)+"...Devamını paylaşımda oku!")
  } 
     
  console.log("Data girdi") 
  $(".postsMain").append(`<div class="postsAlt">
<h3>${item.title}</h3><p class="aciklama">${content}</p>
<br /><p class="paylasimci">Paylaşan: ${item.sharer}</p><p class="tarih">Paylaşma zamanı: ${item.date}</p><button class="gitmeButton"><a class="git" href="https://endorfintr-blog.glitch.me/posts/${veri.ID}">Gönderiye git!</a></button></div>`);
  console.log('Başarıyla data çekildi'); 

});
  });
});

I want a certain sentence interrupted when it exceeds a certain character limit, yes I used substr but it becomes ineffective when I use the if function

JS

$(function () {
  $.getJSON("/admin/getposts", function (data) {
    $(".postsMain").empty();
    $.each(data.reverse(), function (v, veri) {
    let item = veri.data
    let content = item.content
    if(content > 70) {
    const generalContent = content.set((content).substr(0,110)+"...Devamını paylaşımda oku!")
  } 
     
  console.log("Data girdi") 
  $(".postsMain").append(`<div class="postsAlt">
<h3>${item.title}</h3><p class="aciklama">${content}</p>
<br /><p class="paylasimci">Paylaşan: ${item.sharer}</p><p class="tarih">Paylaşma zamanı: ${item.date}</p><button class="gitmeButton"><a class="git" href="https://endorfintr-blog.glitch.me/posts/${veri.ID}">Gönderiye git!</a></button></div>`);
  console.log('Başarıyla data çekildi'); 

});
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千年*琉璃梦 2025-01-26 00:10:25

我很关心的问题是,您想在字符限制之后切下最后一个单词之后的句子。因此,如果字符限制为10,并且该句子为“ lorem ipsum dolor sit amet”,则将其切成lorem ipsum之后。如果我对问题不正确,请告诉我,我将删除答案。

我将首先定义单词限制

var wordlimit = 110;

,然后将所有内容进行串联,

var originalSentence = "some form of sentence.";
var endOfSentence = originalSentence.substring(wordlimit,originalSentence.length);

然后我会找出多少个字符,直到下一个whitespace,

var charactersUntilNextWord = endOfSentence.indexOf(" ");

然后我将其添加到Wordlimit中,并用新的WordLimit进行新句子。

wordlimit = wordlimit+charactersUntilNextWord;
var newSentence = originalSentence.substring(0,wordlimit);

我希望这会有所帮助。相反,如果您想在一定数量的单词之后切下句子,我建议您在whitespace(“”)上将句子分开,然后将所有单词添加到该词limit,直到该词limit到新字符串。

I am asuming the question is that you want to cut the sentence of after the final word, after the character limit. So if the character limit is 10 and the sentence is "lorem ipsum dolor sit amet" it cuts it of after lorem ipsum. If my understanding of the question is incorrect, tell me and i will delete the answer.

I would start by defining word limit

var wordlimit = 110;

Then make a substring with everything after that

var originalSentence = "some form of sentence.";
var endOfSentence = originalSentence.substring(wordlimit,originalSentence.length);

Then i would find out how many characters until next whitespace

var charactersUntilNextWord = endOfSentence.indexOf(" ");

Then i would add that to the wordlimit and make a new sentence with new wordlimit.

wordlimit = wordlimit+charactersUntilNextWord;
var newSentence = originalSentence.substring(0,wordlimit);

I hope this helps. If you are instead looking to cut the sentence after a certain number of words i would advise you to split the sentence on whitespace (" ") and then add all the words until that wordlimit to a new string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文