首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您如何截断html文本的反应?

您如何截断html文本的反应?
EN

Stack Overflow用户
提问于 2021-01-08 10:24:21
回答 4查看 1.6K关注 0票数 3

我试图将以下html截断为5个字符:

代码语言:javascript
复制
<b>Hello</b> how are you today?

我要找的结果是:

Hello

但是,如何忽略截断中的html标记,使其不会产生以下结果?

代码语言:javascript
复制
<b>He

我使用的是html解析器,因此不能在使用该解析器后截断字符串。这是我的代码,不用说,不起作用!

代码语言:javascript
复制
import React from 'react';
import parse from 'html-react-parser';
import Typography from '@material-ui/core/Typography';


const Message= () => {
  const message= "<b>Hello</b> how are you today?"
  const messageParsed = parse(message);

  return (
          <Typography variant="body2">
            {messageParsed.substr(0, 5)}
          </Typography>
  );
};

export default Message;

正确的方法是什么?

非常感谢,

凯蒂

EN

回答 4

Stack Overflow用户

发布于 2021-01-08 13:19:40

您可以使用s来保持html结构

要获得具有一定长度的html,请调用下面的htmlToLength(html, length)

您还可以通过调用DocumentFragment来获得htmlToNodeWithLength(html, length)

这个算法的核心是findRangeWithLength(range, length),它将range从末尾(递归)缩小,直到它有给定的length

代码语言:javascript
复制
function htmlToLength(html, length) {
  const trimmedNode = htmlToNodeWithLength(html, length);

  const container = document.createElement("div");
  container.appendChild(trimmedNode);
  return container.innerHTML;
}

function htmlToNodeWithLength(html, length) {
  // Only for measurement. Never added to DOM.
  const container = document.createElement("div");
  container.innerHTML = html;

  const fullRange = document.createRange();
  fullRange.setStart(container, 0);
  fullRange.setEnd(container, 1);

  const range = findRangeWithLength(fullRange, length);
  return range.cloneContents();
}

function findRangeWithLength(range, length) {
  if (rangeLength(range) < length) return range;

  // Find the childNode with at least length content.
  for (const childNode of range.endContainer.childNodes) {
    range.setEnd(childNode, lastEndOffset(childNode));
    if (rangeLength(range) >= length) {
      return findRangeWithLength(range, length);
    }
  }

  // There are no child nodes long enough. It's a text node.
  const diff = length - rangeLength(range) + range.endOffset;
  range.setEnd(range.endContainer, diff);
  return range;
}

function lastEndOffset(node) {
  return node.childNodes.length || node.textContent.length;
}

function rangeLength(range) {
  return range.toString().length;
}

const html = "<p>No <span></span><b>Hello</b> <i>World</i></p>";
const length = 7;
const trimmedNode = htmlToNodeWithLength(html, length);

document.querySelector(".raw-input").textContent = html;
document.querySelector(".trimmed").appendChild(trimmedNode);
document.querySelector(".length").textContent = length;
document.querySelector(".input").innerHTML = html;
document.querySelector(".raw-output").textContent = htmlToLength(html, length);
代码语言:javascript
复制
<h2>Raw HTML input</h2>
<div class="raw-input"></div>
<h2>Rendered Input</h2>
<div class="input"></div>
<h2>Rendered output to length <span class="length">?</span></h2>
<div class="trimmed"></div>
<h2>Raw HTML output</h2>
<div class="raw-output"></div>

票数 1
EN

Stack Overflow用户

发布于 2022-10-04 23:08:31

如果您不想按字符大小截断,那么下面可以帮助您

  • 1行:
代码语言:javascript
复制
p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
  • 多行(例如: 3)
代码语言:javascript
复制
p {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

代码语言:javascript
复制
p {
  height: 4.5rem;
  line-height: 1.5rem;
  overflow: hidden;
}

如果要按字符大小截断

代码语言:javascript
复制
div {
  /* <div><p></p></div> */
  width: '50px'; /* equal pixels to characters width */
  overflow: hidden;
}
票数 1
EN

Stack Overflow用户

发布于 2021-01-08 12:47:10

据我所知,您有一个HTML文本,需要执行以下操作

  1. 删除HTML标记
  2. 在步骤1到步骤5之后截断字符串。

在这种情况下,可以使用正则表达式来完成。

代码语言:javascript
复制
import React from "react";
import "./styles.css";

export default function App() {
  const message = "<b>Hello</b> how are you today?";
  const messageString = message.replace(/<(.|\n)*?>/g, '');

  const subText = messageString.substring(0, 5);

  return <div>{subText}</div>;
}

检查这里中的工作样本。

查看 & 链接,了解更多关于如何从JS中的字符串中移除标记的信息。

对于包html-react解析器,这不会返回字符串,因此substring方法将无法工作。它返回一个数组。在您的示例中,行parse(<b>Hello</b> how are you today?);将返回以下内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65627253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档