用JavaScript将我的描述中的字符限制为140个字符

发布于 2025-02-10 23:36:51 字数 646 浏览 1 评论 0原文

我正在研究一个项目,该项目需要使用API​​ URL将书籍列表打印到页面上。

代码构成奇迹,但我正在努力将字符限制在140中,以获取所需的描述。

function renderItemHtml(item) {
return `<div class="book"><img src="${item.volumeInfo.imageLinks.thumbnail}" class="thumbnails" 
alt="${item.singleTitle} by ${item.volumeInfo.authors[0]}" />
<div>
<h4>${item.volumeInfo.title}</h4>
<p><strong>${item.volumeInfo.authors}</strong></p>
<h8>${item.volumeInfo.description}</h8>        <-------- what i need to limit
</div>
<br>
<h8>Pages:  ${item.volumeInfo.pageCount}</h8>
</div>`

}

I'm working on a project that requires a list of books to be printed onto the page using an api url.

Code works wonders, but I'm struggling to limit the characters to 140 for the description as requested.

function renderItemHtml(item) {
return `<div class="book"><img src="${item.volumeInfo.imageLinks.thumbnail}" class="thumbnails" 
alt="${item.singleTitle} by ${item.volumeInfo.authors[0]}" />
<div>
<h4>${item.volumeInfo.title}</h4>
<p><strong>${item.volumeInfo.authors}</strong></p>
<h8>${item.volumeInfo.description}</h8>        <-------- what i need to limit
</div>
<br>
<h8>Pages:  ${item.volumeInfo.pageCount}</h8>
</div>`

}

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

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

发布评论

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

评论(1

七度光 2025-02-17 23:36:51

只是这样做:

<h8>${item.volumeInfo.description.slice(0,140)}</h8> 

Just do this:

<h8>${item.volumeInfo.description.slice(0,140)}</h8> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文