小程序富文本解析了多个video,如何做到只能一个播放 其它video暂停播放 没法给video加ref和点击事件 因为是富文本编辑器

<div v-html="info.brief" class="content" ref="editor" @click="judgeImg($event)"></div>

<script>
export default {
  mounted() {
    const observer = new MutationObserver(this.addVideoListeners);
    const editorElement = this.$refs.editor; // 假设富文本编辑器有一个ref="editor"
    observer.observe(editorElement, { childList: true, subtree: true });
  },
  beforeDestroy() {
    // 清除MutationObserver
    const observer = new MutationObserver(this.addVideoListeners);
    const editorElement = this.$refs.editor;
    observer.disconnect();
  },
  methods: {
    addVideoListeners(mutations) {
      mutations.forEach(mutation => {
        mutation.addedNodes.forEach(node => {
          if (node.tagName.toLowerCase() === 'video') {
            // 为新添加的video元素添加事件监听器
            node.addEventListener('play', this.pauseOtherVideos);
          }
        });
      });
    },
    pauseOtherVideos(event) {
      const videos = this.$el.querySelectorAll('video');
      videos.forEach(video => {
        if (video !== event.target) {
          video.pause();
        }
      });
    }
  }
};
</script>

主要是富文本编辑器里的视频 所以比较难折腾

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Vue和Element UI来实现一个富文本编辑器,同时也可以使用第三方库Quill来实现。以下是使用Element UI和Quill实现的代码示例: 使用Element UI: 1. 首先需要安装Element UI和Quill: ``` npm install element-ui quill --save ``` 2. 在Vue组件中引入Element UI和Quill: ```javascript import { quillEditor } from 'vue-quill-editor' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' import { Button, Input } from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' ``` 3. 在Vue组件中使用Element UI和Quill: ```html <template> <div> <el-button type="primary" @click="bold">粗</el-button> <el-button type="primary" @click="red">标红</el-button> <quill-editor v-model="content" :options="editorOption"></quill-editor> </div> </template> <script> export default { components: { quillEditor, 'el-button': Button, 'el-input': Input }, data() { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['link', 'image', 'video'] ] } } } }, methods: { bold() { const quill = this.$refs.myQuillEditor.quill const range = quill.getSelection() if (range) { quill.format('bold', true) } }, red() { const quill = this.$refs.myQuillEditor.quill const range = quill.getSelection() if (range) { quill.format('color', 'red') } } } } </script> ``` 使用Quill: 1. 首先需要安装Quill: ``` npm install quill --save ``` 2. 在Vue组件中引入Quill: ```javascript import Quill from 'quill' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' ``` 3. 在Vue组件中使用Quill: ```html <template> <div> <button @click="bold">粗</button> <button @click="red">标红</button> <div ref="editor"></div> </div> </template> <script> export default { data() { return { editor: null } }, mounted() { this.editor = new Quill(this.$refs.editor, { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['link', 'image', 'video'] ] }, theme: 'snow' }) }, methods: { bold() { const range = this.editor.getSelection() if (range) { this.editor.format('bold', true) } }, red() { const range = this.editor.getSelection() if (range) { this.editor.format('color', 'red') } } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值