場景:
1. 在vue中, 又在for循環中, 給div元素配置contenteditable屬性,? 但是使用不了v-model綁定
2. 點擊外部元素需要聚焦并將光標聚焦到最后位置
方案:?
1. 使用vue-input-contenteditable第三方包, 可以使用v-model綁定,?
// 下載
yarn add vue-input-contenteditable// 導入并引用
<template><input-contenteditablev-model="myModel"id="id"_is="p":placeholder="myPlaceHolder":maxlength="25" />
</template><script>
import InputContenteditable from 'vue-input-contenteditable';export default {components: {'input-contenteditable': InputContenteditable},data: {myModel: '',myPlaceholder: 'Type your data here...'}
};
</script>
2.?點擊外部元素需要聚焦并將光標聚焦到最后位置
// 光標聚焦到末尾
const dom = document.getElementById('id')
dom && dom.focus()
document.execCommand('selectAll', false, null);
document.getSelection().collapseToEnd();