当前位置:首页 > VUE

vue实现文本选取

2026-01-16 18:42:23VUE

文本选取的基本实现

使用Vue实现文本选取功能可以通过结合原生JavaScript的Selection API完成。以下是一个基础示例,通过指令或方法监听文本选择事件:

<template>
  <div @mouseup="handleTextSelection" ref="selectableText">
    {{ content }}
  </div>
  <p>已选文本: {{ selectedText }}</p>
</template>

<script>
export default {
  data() {
    return {
      content: '这是一段可供选择的示例文本...',
      selectedText: ''
    }
  },
  methods: {
    handleTextSelection() {
      const selection = window.getSelection();
      this.selectedText = selection.toString().trim();
    }
  }
}
</script>

自定义指令实现

创建自定义指令v-select可以复用文本选择逻辑:

Vue.directive('select', {
  bind(el, binding) {
    el.addEventListener('mouseup', () => {
      const selection = window.getSelection();
      const selectedText = selection.toString().trim();
      if (selectedText) {
        binding.value(selectedText);
      }
    });
  }
});

使用方式:

<div v-select="handleSelectedText">{{ content }}</div>

高亮选中文本

通过Range API实现选中文本的高亮显示:

methods: {
  highlightSelection() {
    const selection = window.getSelection();
    if (!selection.rangeCount) return;

    const range = selection.getRangeAt(0);
    const span = document.createElement('span');
    span.className = 'highlight';
    range.surroundContents(span);
    selection.removeAllRanges();
  }
}

CSS样式:

.highlight {
  background-color: yellow;
  color: black;
}

跨浏览器兼容处理

不同浏览器对Selection API的实现存在差异,需要做兼容处理:

getSelectedText() {
  if (window.getSelection) {
    return window.getSelection().toString();
  } else if (document.selection) {
    return document.selection.createRange().text;
  }
  return '';
}

文本选择事件扩展

添加防抖和选择边界检测:

handleTextSelection: _.debounce(function() {
  const selection = window.getSelection();
  if (!selection.isCollapsed) {
    const range = selection.getRangeAt(0);
    if (this.$refs.selectableText.contains(range.commonAncestorContainer)) {
      this.selectedText = selection.toString();
    }
  }
}, 300)

服务端交互示例

将选中文本发送至后端:

methods: {
  async sendSelectionToServer() {
    const text = this.getSelectedText();
    if (!text) return;

    try {
      await axios.post('/api/selected-text', { text });
      this.$toast.success('文本已保存');
    } catch (error) {
      console.error(error);
    }
  }
}

移动端触摸支持

针对移动设备添加touch事件支持:

vue实现文本选取

mounted() {
  this.$refs.selectableText.addEventListener('touchend', this.handleTouchSelection);
},
methods: {
  handleTouchSelection() {
    setTimeout(() => {
      const selection = window.getSelection();
      this.selectedText = selection.toString();
    }, 100);
  }
}

标签: 文本vue
分享给朋友:

相关文章

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition 组件…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…