限制JS生成的HTML标签

发布于 2025-01-19 17:06:28 字数 1773 浏览 0 评论 0原文

下面的 JS 代码从输入中捕获单词作为主题标签,并将它们转换为 HTML span 标签。我只需要限制用户生成的单词和跨度标签的数量。例如10个字。此外,通过按空格键,用户可以删除标签。

const BACKSPACE = 8;
const ENTER = 32;
document.getElementById('tag-input').addEventListener('keydown', adjust);

function adjust(e) {
  const val = e.target.value;

  if (e.keyCode === BACKSPACE && !val) {
    deleteTag();
  }

  if (e.keyCode === ENTER && val) {
    e.target.value = '';
    addTag(val);
  }

  const textLength = textLengthToPx(val);
  const inputWidth = e.target.offsetWidth;
  const minThreshold = 1;
  const maxThreshold = 200;
  const delta = inputWidth - textLength;
  const shouldGrow = delta < minThreshold;
  const shouldShrink = delta > maxThreshold;

  if (shouldGrow) {
    setStyle(e.target, 'width', '90%');
  } else if (shouldShrink) {
    e.target.style = '';
  }
}

function deleteTag() {
  document.querySelectorAll('#tags > span')[document.querySelectorAll('#tags > span').length - 1].remove();
}

function addTag(val) {
  const input = document.getElementById('tag-input');
  const tag = document.createElement('span');
  const tagsContainer = document.getElementById('tags');
  tag.className = 'tag';
  tag.innerHTML = val;
  let counter = 0;
  while (counter <= 5) {
    tagsContainer.insertBefore(tag, input);
    counter++;
  }
}

function setStyle(node, rule, value) {
  node.style = `${rule}:${value}`;
}

function textLengthToPx(text) {
  const span = document.createElement('span');
  span.innerHTML = text;
  span.className = 'invisible';
  return span.offsetWidth;
}
<textarea id="tag-input"></textarea>

JS code below catches words from an input as hashtags and turn them to HTML span tag. I just need to put limitation at the amount of words and span tags that are generated by user. For example 10 words. Moreover, by pressing space button the user can delete tags.

const BACKSPACE = 8;
const ENTER = 32;
document.getElementById('tag-input').addEventListener('keydown', adjust);

function adjust(e) {
  const val = e.target.value;

  if (e.keyCode === BACKSPACE && !val) {
    deleteTag();
  }

  if (e.keyCode === ENTER && val) {
    e.target.value = '';
    addTag(val);
  }

  const textLength = textLengthToPx(val);
  const inputWidth = e.target.offsetWidth;
  const minThreshold = 1;
  const maxThreshold = 200;
  const delta = inputWidth - textLength;
  const shouldGrow = delta < minThreshold;
  const shouldShrink = delta > maxThreshold;

  if (shouldGrow) {
    setStyle(e.target, 'width', '90%');
  } else if (shouldShrink) {
    e.target.style = '';
  }
}

function deleteTag() {
  document.querySelectorAll('#tags > span')[document.querySelectorAll('#tags > span').length - 1].remove();
}

function addTag(val) {
  const input = document.getElementById('tag-input');
  const tag = document.createElement('span');
  const tagsContainer = document.getElementById('tags');
  tag.className = 'tag';
  tag.innerHTML = val;
  let counter = 0;
  while (counter <= 5) {
    tagsContainer.insertBefore(tag, input);
    counter++;
  }
}

function setStyle(node, rule, value) {
  node.style = `${rule}:${value}`;
}

function textLengthToPx(text) {
  const span = document.createElement('span');
  span.innerHTML = text;
  span.className = 'invisible';
  return span.offsetWidth;
}
<textarea id="tag-input"></textarea>

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

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

发布评论

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

评论(1

谁的年少不轻狂 2025-01-26 17:06:28

通过添加计数器解决了问题。

Solved the problem by adding a counter.

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