HTML DOM Style textOverflow 属性

htmlweb developmentfront end technology更新于 2025/6/9 6:04:17

DOM Style textOverflow 属性返回并应用当文本在 HTML 文档中超出元素容器范围时应发生的情况。

语法

以下是语法 −

  • 返回 textOverflow

object.style.textOverflow
  • 修改 textOverflow

object.style.textOverflow = “value”

值

此处,值可以为 −

值说明
inherit从其父元素继承此属性值。
initial将此属性值设置为其默认值。
clip剪切溢出的文本。
ellipsis设置省略号("...")来表示元素被剪切的文本。
string设置给定的字符串,用于表示元素的剪切文本。

示例

让我们看一个 Style textOverflow 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      background: lightblue;
      height: 100vh;
   }
   p {
      margin: 1.5rem auto;
      border: 3px solid #fff;
      width: 400px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: visible;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
   }
</style>
</head>
<body>
<h1>DOM Style textOverflow Property Example</h1>
<p>
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
</p>
<button onclick="add()" class="btn">Change textOverflow</button>
<script>
   function add() {
      document.querySelector('p').style.textOverflow = "ellipsis";
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"更改文本溢出"按钮可更改溢出段落文本的行为。省略号将在此处添加 −


相关文章


有用资源