关键代码

        <div class="dialog-main"
:contenteditable= "editable"
v-text="noticeContent"
v-if="noDate"
innerHTML.length = "20"
ref="Maincontent"
@input="handleInput"
@compositionstart="handleStart"
@compositionend="handleEnd"
> /**
* 限制输入长度
* 如10个中文,英文就可以20
*/
validateTextLength (value) {
// 中文、中文标点、全角字符按1长度,英文、英文符号、数字按0.5长度计算
let cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g
let mat = value.match(cnReg)
let length
if (mat) {
length = (mat.length + (value.length - mat.length) * 0.5)
return length
} else {
return value.length * 0.5
}
},
/**
* @input事件
*/
handleInput (event) {
let text = event.target.innerText
this.valueHandle(event, text)
},
/**
* 处理逻辑
* strVale文本数据
*/
valueHandle (event, strVale) {
let _this = this
let text = strVale
this.sendContent = text
if (this.composing) {
return
}
let len = this.validateTextLength(text)
if (len > 500) {
this.$refs.Maincontent.innerHTML = text.substr(0, 500)
this.$refs.Maincontent.focus()
}
setTimeout(() => {
_this.keepLastIndex(event.target)
}, 5)
// 拓展 如果想要只需要前100位数据
// this.content = this.content.substring(0, 100)
},
/**
* 中文输入开始
*/
handleStart () {
this.composing = true
},
/**
* 中文输入结束
*/
handleEnd ($event) {
this.composing = false
let text = $event.target.innerHTML
// console.log($event.target.innerHTML)
this.valueHandle($event, text)
},
// 编辑
editHandle () {
this.editable = true
this.noDate = true
// let notice = this.NoticeContent
// this.NoticeContent(false)
},

全部代码

<template>
<div class="del-confirm" v-if="groupNameStatus">
<div class="group-dialog">
<div class="dialog-content">
<p class="win-top">
<span class="win-txt">群公告</span>
<span class="edit" @click="editHandle" v-if="+userId === +groupCreaterId"><icon-svg name="icon-kaka-compile" icon-style="edit-ico"></icon-svg></span>
<span class="close-icon" @click="closeGroupNameDialog">
<icon-svg name="icon-kaka-close" icon-style="off-ico"></icon-svg>
</span>
</p>
<div class="dialog-main"
:contenteditable= "editable"
v-text="noticeContent"
v-if="noDate"
innerHTML.length = "20"
ref="Maincontent"
@input="handleInput"
@compositionstart="handleStart"
@compositionend="handleEnd"
>
</div>
<!-- <div>{{groupAnnouncement}}</div> -->
<div v-if="!noDate" class="dialog-main">暂无群公告</div>
<div class="name-create-btn" v-if="editable">
<div class="cancel" @click="cancelEdit">取消</div>
<div class="sure" @click="sure">确定</div>
</div>
<div class="edit-tips" v-if="!editable"><p class="tips-main">只有群主才能修改公告</p></div>
</div>
</div>
</div>
</template> <script>
import api from '@/api/group'
export default {
name: 'notice',
showChatBox: {
type: Boolean,
default: false
},
data () {
return {
groupNameStatus: true,
friendsInfo: this.$store.getters.friendsInfo,
contactList: {},
checkedContact: [],
searchVal: '',
groupName: '',
userId: this.$store.getters.userId,
noticeContent: '',
editable: false,
noDate: false,
sendContent: '',
composing: false
}
},
computed: {
// 群id
groupId () {
return this.$store.state.mainInit.showGroupNoticeId.groupId
},
// 群id
groupCreaterId () {
return this.$store.getters.groupSetInfo.createrId
},
groupAnnouncement () {
return this.$store.getters.groupSetInfo.groupAnnouncement
},
NoticeContent: {
get () {
let groupAnnouncement = this.$store.getters.groupSetInfo.groupAnnouncement
if (groupAnnouncement) {
this.noDate = true
this.noticeContent = groupAnnouncement
}
return groupAnnouncement
},
set (v) {
// 使用vuex中的mutations中定义好的方法来改变
let groupNotice = this.$store.state.mainInit.showGroupNoticeId
let copyMyinfo = Object.assign({}, groupNotice)
copyMyinfo.groupInfo.groupAnnouncement = v
console.log(copyMyinfo)
this.$store.dispatch('showGroupNoticeId', copyMyinfo)
}
}
},
methods: {
/**
* 限制输入长度
* 如10个中文,英文就可以20
*/
validateTextLength (value) {
// 中文、中文标点、全角字符按1长度,英文、英文符号、数字按0.5长度计算
let cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g
let mat = value.match(cnReg)
let length
if (mat) {
length = (mat.length + (value.length - mat.length) * 0.5)
return length
} else {
return value.length * 0.5
}
},
/**
* @input事件
*/
handleInput (event) {
let text = event.target.innerText
this.valueHandle(event, text)
},
/**
* 处理逻辑
* strVale文本数据
*/
valueHandle (event, strVale) {
let _this = this
let text = strVale
this.sendContent = text
if (this.composing) {
return
}
let len = this.validateTextLength(text)
if (len > 500) {
this.$refs.Maincontent.innerHTML = text.substr(0, 500)
this.$refs.Maincontent.focus()
}
setTimeout(() => {
_this.keepLastIndex(event.target)
}, 5)
// 拓展 如果想要只需要前100位数据
// this.content = this.content.substring(0, 100)
},
/**
* 中文输入开始
*/
handleStart () {
this.composing = true
},
/**
* 中文输入结束
*/
handleEnd ($event) {
this.composing = false
let text = $event.target.innerHTML
// console.log($event.target.innerHTML)
this.valueHandle($event, text)
},
// 编辑
editHandle () {
this.editable = true
this.noDate = true
// let notice = this.NoticeContent
// this.NoticeContent(false)
},
closeCreateGroupWindow () {
this.$electron.ipcRenderer.send('closeCreateGroup')
},
// 取消
cancelEdit () {
this.editable = false
if (this.noticeContent) {
this.noDate = true
} else {
this.noDate = false
}
},
// 关闭
closeGroupNameDialog () {
this.$store.dispatch('showGroupNotice', false)
this.editable = false
},
// 确定保存
sure () {
// console.log(this.noticeContent) 公告接口测试修改
let groupId = this.$store.state.mainInit.showGroupNoticeId.groupId
let params = {
userId: this.$store.getters.userId,
groupId: groupId,
groupAnnouncement: this.sendContent,
devType: 3
}
api.sendAnnouncement(params).then(res => {
console.log(res)
if (res.code === '1') {
this.$store.dispatch('showGroupNotice', false)
// 保存到当前的VUEX
let groupSetInfo = this.$store.getters.groupSetInfo
let copyMyinfo = Object.assign({}, groupSetInfo)
copyMyinfo.groupAnnouncement = this.sendContent
this.$store.dispatch('groupSetInfo', copyMyinfo)
}
this.editable = false
// this.$store.commit('SHOW_HANDLESUCCESS', true)
}).catch(err => {
console.log(err)
})
},
keepLastIndex (obj) {
if (window.getSelection) { // ie11 10 9 ff safari
obj.focus() // 解决ff不获取焦点无法定位问题
let range = window.getSelection() // 创建range
range.selectAllChildren(obj) // range 选择obj下所有子内容
range.collapseToEnd() // 光标移至最后
} else if (document.selection) { // ie10 9 8 7 6 5
let range = document.selection.createRange() // 创建选择对象
// var range = document.body.createTextRange();
range.moveToElementText(obj) // range定位到obj
range.collapse(false) // 光标移至最后
range.select()
}
},
getNoticeData () {
let groupAnnouncement = this.$store.getters.groupSetInfo.groupAnnouncement
if (groupAnnouncement) {
this.noDate = true
this.noticeContent = groupAnnouncement
}
}
},
created () {
// this.dealContactList()
this.getNoticeData()
}
}
</script> <style scoped lang="scss">
.del-confirm {
position: absolute;
width: 100%;
height: 100%;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:rgba(0, 0, 0, 0);
z-index: 999;
.win-top {
height: 40px;
background-color: #F5F6F7;
padding-left: 20px;
border-radius: 4px;
// @extend .drag;
.win-txt {
float: left;
font-size: 14px;
color: #252B38;
line-height: 40px;
// @extend .no-drag;
}
.edit{
width: 15px;
height: 14px;
line-height: 40px;
margin-left: 10px;
cursor: pointer;
}
.close-icon {
float: right;
width: 30px;
height: 30px;
margin-top: 5px;
margin-right: 5px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
.iconsvg {
color: #A4A4A7;
font-size: 9px;
}
&:hover {
& > .off-ico {
color: #f45454;
}
}
@extend .no-drag;
}
}
.group-dialog {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background: $rgba;
z-index: 1;
.dialog-content {
width: 338px;
height: 440px;
position: absolute;
background-color: #fff;
left: 50%;
top: 50%;
margin-left: -169px;
margin-top: -220px;
box-shadow: 0 2px 10px 2px rgba(0, 0, 0, .19);
border-radius: 4px;
.dialog-main {
height: 350px;
padding: 20px;
overflow: auto;
// -webkit-user-modify: read-write-plaintext-only;
outline: none;
[contenteditable]:empty:not(:focus):before{
content: attr(placeholder)
}
&:focus {
outline: none;
}
}
}
.group-name {
width: 213px;
height: 22px;
border: 1px solid #E2E6E9;
font-size: 12px;
color: #232936;
padding: 0 7px;
display: block;
margin: 22px auto 35px;
&:focus {
outline: none;
}
}
.name-create-btn {
text-align: center;
// height: 30px;
font-size: 0;
.cancel {
display: inline-block;
width: 68px;
height: 30px;
line-height: 30px;
text-align: center;
border-radius: 4px;
font-size: 12px;
cursor: pointer;
background-color: #ffffff;
border: 1px solid #999;
color: #333;
margin-right: 15px;
}
.sure{
display: inline-block;
margin-left: 15px;
width: 68px;
height: 30px;
line-height: 30px;
text-align: center;
border-radius: 4px;
font-size: 12px;
cursor: pointer;
background-color: $btn-confim;
color: #fff;
border: 1px solid $btn-confim;
}
}
.edit-tips {
text-align: center;
height: 30px;
line-height: 30px;
.tips-main{
font-size:10px;
color:rgba(162,164,168,1);
position: relative;
&::after,&::before {
content: '';
width: 20px;
height: 1px;
background:rgba(216,216,216,1);
border-radius: 1px;
position: absolute;
}
&::after {
left: 90px;
top: 15px;
}
&::before {
right: 90px;
top: 15px;
}
}
}
}
}
</style>

VUE DIV模拟input框的基本处理的更多相关文章

  1. div模拟文本框textarea

    需求:利用highlight.js对文本框中的内容进行高亮显示 1.highlight.js使用 js中:<script src="js/highlight/highlight.pac ...

  2. vue中让input框自动聚焦

    created(){ this.changfouce(); }, methods: { //在vue生命周期的created()钩子函数进行的DOM操作要放在Vue.nextTick()的回调函数中, ...

  3. input框只允许输入正整数、正数(包含小数)的解决方法 vue.js实现

    我来打自己脸了!!!!...刚刚发现在中文输入法下是无效的,有人能解决这个问题么 如果要求input只能输入数字怎么做? 设置type="number" ? 那我如果想限制长度,此 ...

  4. vue开发中利用正则限制input框的输入(手机号、非0开头的正整数等)

    我们在前端开发中经常会碰到类似手机号输入获取验证码的情况,通常情况下手机号的输入需要只能输入11位的整数数字.并且需要过滤掉一些明显不符合手机号格式的输入,那么我们就需要用户在输入的时候就控制可以输入 ...

  5. jquery div 下拉框焦点事件

    这章与上一张<jquery input 下拉框(模拟select控件)焦点事件>类似 这章讲述div的焦点事件如何使用 div的焦点事件与input的焦点事件区别在于 需要多添加一个属性: ...

  6. 小实例---关于input宽度自适应以及多个input框合并拆分

    前两个月,公司内部需要开发关于大数据方面的辅助工具语料分词系统,在这个项目中遇到以下几个主要问题,在此分享~ 一.input宽度根据内定文本宽度自适应 背景:项目需求中,前台展示,需要从后台获取的.t ...

  7. 使用DIV弹出框的代码示例,备忘。

    1.思路 使用DIV模拟弹出框,一共用三个div: divWindow: 原来的界面内容区域 divLogin:要弹出的内容区域 divBackground:给弹出内容区域做个遮罩的区域. 点击 “请 ...

  8. vue.js实现单选框、复选框和下拉框

    Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势.下边以单选框.复选框和下拉框为例介绍他们在HTML和Vue.js中的具体实现方式. 一.单选框   在传统的HTM ...

  9. VUE 单选下拉框Select中动态加载 默认选中第一个

    <lable>分类情况</lable> <select v-model="content.tid"> <option v-for=&quo ...

随机推荐

  1. poatman接口测试--初试

    接到测试任务,对两个商品接口,进行接口测试 测试工具:postman 域名:rap2查找的或询问开发, 接口的参数规则:参考rap2的备注 开发没有添加详细说明的,让开发补充说明规则,及定义的返回状态 ...

  2. TypeError: reduction operation 'argmax' not allowed for this dtype

    这个错误真的tmd伤脑筋.我用idxmax函数去求series类型的最大值的索引,结果明明是下面这种数据, 无论我如何pint他的shape,type,他怎么看都是一个满足idxmax函数要求的参数类 ...

  3. Jmeter+TCP\Sockets(8583)报文压力测试

    Jmeter一般被用来测试HTTP协议,我第一次拿来测试socket协议,pos机传输报文为8583,协议属于socket,也是TCP协议的一种,网上有LR怎么测试8583报文,我就研究了一下怎么用J ...

  4. 【VS开发】【图像处理】相机中白平衡的算法模拟实现

    相机主要技术点为3A算法. 而3A算法主要指的是自动对焦(AF).自动曝光(AE)及自动白平衡(AWB).自动白平衡:根据光源条件调整图片颜色的保真程度. 网上时常有类似招聘如下的招聘信息: ---- ...

  5. 第j九周学习总结暨第七周实验报告

    完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 一:实验代码 package d ...

  6. Luogu P1650 田忌赛马

    题目 如果我们最大比对面最大大,那么直接用. 如果我们最小比对面最小大,那么直接用. 否则用我们最小去换对面最大. #include<bits/stdc++.h> using namesp ...

  7. c++ vector 使用注意事项

    1. 初始化 c++ 11以后新增了大括号{}的初始化方式,需要注意与()的区别,如: std::vector<int> vecTest1(5);         //初始化5个元素,每个 ...

  8. C++中的异常处理(上)

    1,C++ 内置了异常处理的语法元素 try ... catch ...: 1,try 语句处理正常代码逻辑: 2,catch 语句处理异常情况: 3,try 语句中的异常由对应的 catch 语句处 ...

  9. shell with hadoop

    shell 命令操作 hadoop 之前多少提及过,这里做个总结. shell with hdfs 基本命令 bin/hadoop fs 大于下面的命令 bin/hdfs dfs dfs 是 fs 的 ...

  10. [Vue] vue-router-interview

    1.vue-router 怎么重定向页面? 路由中配置 redirect 属性 使用路由的别名来完成重定向 2.vue-router 怎么配置 404 页面? path: '*',放在最后一个 3.切 ...