首页 > 其他分享 >前端划词选中实现

前端划词选中实现

时间:2023-10-12 10:35:20浏览次数:20  
标签:font target 前端 ruleForm tag 选中 tagName 划词 txt

image

1、需求

如上图,鼠标划词选中标签,选中的词自动填入到输入框中

2、实现

    <div class="tag-words" @mouseup="startSelectWord">
      <div class="tag-word" v-for="(item, index) in tagWords" :key="index">
        {{ item.text }}
      </div>
    </div>

    startSelectWord(event) {
      const target = event.target;
      if (target.classList.contains('tag-words') || target.classList.contains('tag-word')) {
        var txt;
        if (window.getSelection) {
          txt = window.getSelection();
        } else {
          txt = document.selection.createRange().text;
        }
        if (txt + '' !== '') {
          this.ruleForm.tagName = (txt + '').replace(/(\r\n|\n|\r)/gm, '');
        }
        console.log(this.ruleForm.tagName, 'ruleForm')
      }
    }

注意:因为划词选中的效果只针对于class="tag-words"和class="tag-word"这个div里,所以需要进行一个event.target的判断
如下图class="tag-words"的div是红框部分,class="tag-word"的div是每个词的部分

image

3、实现效果
image

image

image

4、全部代码

<template>
  <div>
    <el-dialog
      :title="dialogVisibleTitle"
      :visible.sync="dialogVisible"
      width="50%"
      center
      :before-close="handleClose"
    >
      <div class="formClass">
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          label-width="95px"
          class="demo-ruleForm"
        >
          <div class="note">
            <i class="el-icon-warning"></i>第一个选择的标签将默认为主标签。注:产品标签将和预警数据进行关联,请不要选择通用性词汇,如:理财计划、二十三期等。
          </div>
          <br>
          <div class="prompt">
            <p>请选择标签</p>
            <p>可划词添加标签</p>
          </div>
          <div class="tag-words" @mouseup="startSelectWord">
            <div class="tag-word" v-for="(item, index) in tagWords" :key="index">
              {{ item.text }}
            </div>
          </div>
          <br>
          <el-input style="width: 500px" v-model="ruleForm.tagName"/>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="conformFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {participle} from "@/api/apply/apply";

export default {
  data() {
    return {
      dialogVisibleTitle: '添加标签',
      dialogVisible: false,
      ruleForm: {
        tagName: '',
        eventTag: ''
      },
      tagArray: [],
      tagWords: [],
      isSelecting: false,
      selectedWords: []
    }
  },
  methods: {
    conformFn() {
      this.$parent.tagNames.push({tagName: this.ruleForm.tagName})
      this.handleClose();
    },
    handleClose() {
      this.dialogVisible = false
      this.ruleForm.tagName = ''
    },
    getTagWords(productName) {
      const param =  {
        text: productName
      }
      participle(param).then(res => {
        this.tagWords = res
      })
    },
    startSelectWord(event) {
      const target = event.target;
      if (target.classList.contains('tag-words') || target.classList.contains('tag-word')) {
        var txt;
        if (window.getSelection) {
          txt = window.getSelection();
        } else {
          txt = document.selection.createRange().text;
        }
        if (txt + '' !== '') {
          this.ruleForm.tagName = (txt + '').replace(/(\r\n|\n|\r)/gm, '');
        }
        console.log(this.ruleForm.tagName, 'ruleForm')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .note {
    font-family: Source Han Sans CN;
    font-size: 12px;
    font-weight: normal;
    color: #999999;

    i {
      color: #feb127
    }
  }

  .prompt {
    display: flex;
    align-items: center;

    p:first-child {
      margin-right: 10px;

      font-family: Source Han Sans CN;
      font-size: 14px;
      font-weight: normal;
      color: #333333;
    }

    p:nth-of-type(2) {
      font-family: Source Han Sans CN;
      font-size: 12px;
      font-weight: normal;
      color: #999999;
    }
  }

  .tag-words {
    display: flex;
    gap: 10px;

    font-family: Source Han Sans CN;
    font-size: 12px;
    font-weight: bold;
    line-height: 20px;
    color: #306EF5;
  }
</style>

标签:font,target,前端,ruleForm,tag,选中,tagName,划词,txt
From: https://www.cnblogs.com/cainiaozzw/p/17758898.html

相关文章

  • 2.前端框架(Vue)
    前端框架(Vue)Vue概念;vue是一套前端框架,免除原生JavaScript中的DOM操作,简化书写;基于MVVM(Model-View-ViewModel)思想,实现数据的双向绑定,将编程的关注点放在数据上;官网:https://v2.vuejs.org/框架:是一个半成品软件,是一套可重用的、通用的、软件基础代码模型。基于框架进行开......
  • 开课吧前端1期.阶段3-1:软件的使用 (bower,atom, node)
    今天主要不讲课,试试环境软件:(推荐,有自己熟悉的用自己的)1.编辑器:atom支持插件: atom-typescript language-vue language-weex markdown-preview-plus2.服务器:wamp(apache+mysql)、xampp、nginx web服务器(要能支持php)——Apache 数据库——MySQL3.Phot......
  • Nginx 做前端代理,上传文件速度慢
    同一台服务器,前端部署在nginx,在上传33M的文件时,耗时36s,但是通过IIS或者swagger上传文件,只需要8s解决方案:将Nginx配置文件中 proxy_passhttp://139.196.154.85:54730/;改为localhost: proxy_passhttp://localhost:54730/; 此方案只适用于前后端部署在同一台服务器的情况......
  • 前端大文件上传如何做到刷新续传?
    前言这两天在学习阿里云oss上传。踩了不少坑,终于实现了大文件分片、断点续传的功能。这篇文章主要分享学习笔记,希望能给大家一些帮助。先看效果 技术栈1.前端:react+Ts+axios上传文件2.Node部分:定义接口、阿里云oss3.socket.io:实时同步上传进度特别说明axios中on......
  • luffy前端配置,跨域问题,后端数据库迁移, 后台主页功能,后台管理
    1luffy前端配置......
  • 为什么不能单纯地从前端来解决跨域问题?LiteNVR安防视频系统为何需要跨域?
    视频监控系统涉及到从前端实时图像信息的采集、信息的传输交换、实时监控到信息的数字化、相关的联动应用和庞大的信息管理等多项环节,包括对各前端视频信息的采集、传输、监控、存储、处理、发布、回放、流转、备份等管理。在安防监控系统LiteNVR运维过程中,我们有时会遇到关于跨......
  • 【面试题】2023前端面试真题之JS篇
     世界上只有一种真正的英雄主义,那就是看清生活的真相之后,依然热爱生活。--罗曼罗兰大家好,我是柒八九。在如今的互联网大环境下,每天都充斥着各种负能量。有可能,你上午还在工位摸鱼,下午HR已经给你单独开小灶,很煞有介事的通知你,提前毕业了。在这个浮躁的互联网环境下,总有一种我们永......
  • 【前端面试题】2023年 国庆 前端面试真题之JS篇
    人的一生,总是难免有浮沉。不会永远如旭日东升,也不会永远痛苦潦倒。反复地一浮一沉,对于一个人来说,正是磨练。因此,浮在上面的,不必骄傲;沉在底下的,更用不着悲观。必须以率直、谦虚的态度,乐观进取、向前迈进。——松下幸之助大家好,我是江辰,在如今的互联网大环境下,想必大家都或多或少且有......
  • 微软最热门的10款前端开源项目!
    本文来盘点微软开源的十大前端项目,这些项目在Github上获得了超过45万Star!VisualStudioCodeVisualStudioCode是一款由微软开发的开源的代码编辑器。它支持多种编程语言,如C、C++、C#、Python、JavaScript和TypeScript等,并提供丰富的插件生态系统来扩展功能。VSCode具有......
  • 前端进阶系列——理解 React Ref
    前端进阶系列——理解ReactRef秦书羽杭州@朝夕光年​关注他 17人赞同了该文章Ref是Reference(引用)的缩写。一、前言在React中通常遵循“自上而下”的“单向数据流”。父组件和子组件的通讯只能通过Props。如果要修改一个子组件,我们要修改......