如何在div的右上角添加一个圆圈并显示带有文本溢出的省略号

Posted

技术标签:

【中文标题】如何在div的右上角添加一个圆圈并显示带有文本溢出的省略号【英文标题】:How to add a circle on the top right corner of a div and show ellipsis with text overflow 【发布时间】:2021-06-26 06:33:34 【问题描述】:

我想在使用 jquery 附加到 div 之后显示动态添加的项目的列表。我只希望 div 的最大宽度为 400px。它应该省略文本溢出:隐藏。 div的右上角会有一个徽章。

但是使用text-overflow: hidden,圆圈也被隐藏了。

目前我得到:

但正确的应该显示为:

function buttonClick() 
  var inputValue = $("#inputField").val();

  $(".fileContent").append(
    '<div id="fileList" class="fileName-label">' +
    inputValue +
    '  <div class="badge"><a href="#"  class="close">X</a></div></div>'
  );

  $("#inputField").val(" ");
.fileName-label 
  position: relative;
  display: inline;
  padding: .50rem 0.40rem;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25em;
  background-color: #f0ad4e;


.fileContent 
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  /* position: absolute; */


.fileContent #fileList 
  margin-top: 0.70rem;
  text-overflow: ellipsis;
  overflow: hidden;
  width: auto;
  min-width: 0;
  max-width: 400px;


.badge 
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: -10px;
  right: -15px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  text-align: center;
  padding: 2px;
  background-color: #FF4081;
  font-size: 75%;


.badge :hover 
  background-color: #fc1463;


.close 
  color: #fff;
  text-decoration: none;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" id="inputField" />
<input type="button" value="Click" onclick="buttonClick();return false;" />

<div class="fileContent">
</div>

【问题讨论】:

为什么不为输入值设置一个跨度并添加 text-overflow: hidden 呢? 省略号呢? 我的意思是将其添加到跨度 text-overflow: ellipsis; overflow: hidden; 而不是 div @GowthamRajJ 您的解决方案不起作用,您是否尝试过先检查自己? BOZ 的答案对你有用吗? 【参考方案1】:

如果你不需要在同一个元素上创建两个属性,只需单独编辑文本元素,它的包装是免费的。

function buttonClick() 

  var inputValue = $("#inputField").val();

  $('.fileContent').append('<div id="fileList" class="fileName-label"><div class="text">' + inputValue + '  </div><div class="badge"><a href="#"  class="close">X</a></div></div>');

  $("#inputField").val(" ")

.fileName-label 
  position: relative;


.fileName-label .text 
  padding: .50rem 0.40rem;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  vertical-align: baseline;
  border-radius: 0.25em;
  background-color: #f0ad4e;
  margin-top: 0.70rem;
  width: auto;
  min-width: 0;
  max-width: 400px;


.fileContent 
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  /* position: absolute; */


.badge 
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: -10px;
  right: -15px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  text-align: center;
  padding: 2px;
  background-color: #FF4081;
  font-size: 75%;


.badge :hover 
  background-color: #fc1463;


.close 
  color: #fff;
  text-decoration: none;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" id="inputField" value="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." />
<input type="button" value="Click" onclick="buttonClick();return false;" />

<div class="fileContent">
</div>

【讨论】:

以上是关于如何在div的右上角添加一个圆圈并显示带有文本溢出的省略号的主要内容,如果未能解决你的问题,请参考以下文章

如何制作带有圆形图像和圆形文本的 UI,以及在同一个圆圈上添加评级图标。在 iOS 应用程序中

SwiftUI,列表行添加按钮

向div添加文本内容会导致内联块项目崩溃[重复]

Chrome 在 div 之外溢出 iframe 背景?

如何在 div 框中显示来自 javascript innerhtml 的文本输出,而不会导致文本溢出并且具有 html/css 样式

css溢出文本显示在几行没有分词