vue中使用 contenteditable 制作输入框并使用v-model做双向绑定
<template>
<div class="div-input"
:class="value.length > 0 ? 'placeholder_hide' : ''"
:style="{'min-width': minWidth}"
:contenteditable="input"
:placeholder="placeholder"
@focus="ischecked = true"
@blur="blurFn"
@input="inputFn"
v-html="text"></div>
</template>
<script>
export default {
name: 'DivInput',
props: {
input: {
type: Boolean,
default: true
},
minWidth: {
type: String,
default: '2.2em'
},
placeholder: {
type: String,
default: 'asdasdasda'
},
value: {
type: String,
default: ''
}
},
data () {
return {
ischecked: false,
text: this.value
}
},
mounted () {
},
watch: {
value () {
// 解决光标跳动BUG
if (!this.ischecked) {
this.text = this.value
}
}
},
methods: {
inputFn (e) {
const val = e.target.innerHTML
this.$emit('input', val)
},
blurFn (e) {
this.ischecked = false
this.text = this.value
e.view.blur()
}
}
}
</script>
<style lang="scss">
.div-input {
display: inline-block;
min-width: 4em;
border-bottom: 1px solid #ccc;
outline: none;
padding: 0 5px;
&:focus{
border-color: #333;
}
&:before {
content: attr(placeholder);
white-space: nowrap;
width: 100%;
color: #aaa;
z-index: 0;
cursor: text;
}
&.placeholder_hide {
&:before {
display: none;
}
}
}
</style>
vue中使用 contenteditable 制作输入框并使用v-model做双向绑定的更多相关文章
- vue 中contenteditable="true"添加可编辑属性后v-model双向绑定失效的解决办法
在项目中会遇到需要编辑单元格的双向绑定问题,v-model双向绑定会在添加contenteditable="true"属性后失效解决方法如下,亲测好用(v-html和@blur实现 ...
- Vue中引入TradingView制作K线图
**前言: 本文使用的是1.10版本 , 可通过TradingView.version()查看当前版本. 附上开发文档地址:https://zlq4863947.gitbooks.i...** 一.修 ...
- vue中使用echarts 制作某市各个街道镇的地图
我要制作的是青州的各街道镇的地图,于是我上网搜,很感谢这篇文章的作者给的提点和帮助https://www.jianshu.com/p/7337c2f56876 现在我把自己的制作过程做个整理,以山东省 ...
- WPF中添加一个文本输入框,按Enter回车,执行绑定的Command
在WPF+WMMV模式中使用键盘和鼠标事件的绑定代码如下: <TextBox x:Name="SearchBox" Text="{Binding SearchTex ...
- Vue基础-双向绑定:从 html 到 模板 到 渲染函数
Vue 测试版本:Vue.js v2.5.13 在 Vue 中,可以利用 v-model 语法糖实现数据的双向绑定,例如: <div id="app"> <inp ...
- contenteditable联合v-html实现数据双向绑定的vue组件
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11466197.html 先看最终实现的demo效果图: (1)上面看似文本域的大框是通过给div ...
- 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据
前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...
- Vue中,父组件向子组件传值
1:在src/components/child/文件夹下,创建一个名为:child.vue的子组件 2:在父组件中,设置好需要传递的数据 3:在App.vue中引入并注册子组件 4:通过v-bind属 ...
- vue中的钩子函数的理解
接下来我们对几个钩子函数进行解释 beforeCreated:这个钩子函数实在vue实例创建后,触发的.这个时候还没有进行data里的数据监听和事件的初始化 其实大家很多时候都会在created钩子函 ...
随机推荐
- Appium(Python)API
1.创建新的会话desired_caps = desired_caps = { 'platformName': 'Android', 'platformVersion': '7.0', 'dev ...
- Oracle启动与关闭数据库实例
Oracle数据库启动实例分为3个步骤: 启动实例 加载数据库 打开数据库 通用模式: STARTUP [ nomount | mount | open | force ] [resetrict] ...
- 【outPut_Class 输出类】使用说明
对象:outPut 说明:定义输出结果类的相关操作.此对象的核心是[JSON]类,所以它继承了[JSON]类的所有方法 重要: 输出结果样式为XML时,自带根节点"root".输出 ...
- lintcode 二分查找
题目:二分查找 描述:给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. c ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- Aizu - 2249
注意先保证距离最短,再来判断价格 邻接矩阵回朝内存 ,要用邻接表的 #include<bits/stdc++.h> using namespace std; #define inf 0x ...
- ArrayList与LinkedList的普通for循环遍历
对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] ...
- Caused by: java.lang.NoClassDefFoundError: javax/el/ELManager
出现问题的原因: 在将springboot项目部署到Linux下的Tomcat中,项目无法正常启动(本地能正常运行),Tomcat启动日志中出现: Caused by: java.lang.NoCla ...
- Eclipse 安装SVN、Maven插件
1先安装subeclipse插件就是svn svn - http://subclipse.tigris.org/update_1.6.x 我这里是灰色的说明我安装过了这里只是截图说明下,我就不继续安装 ...
- HADOOP docker(一):安装hadoop实验集群(略操蛋)
一.环境准备 1.1.机器规划 主机名 别名 IP 角色 9321a27a2b91 hadoop1 172.17.0.10 NN1 ZK RM 7c3a3c9cd595 hadoo ...