vue和js文件相互监听DOM内容变化

前言:vue文件引入一个js文件,js中的按钮控制vue  input弹框的出现,vue中的弹框输入的内容需要存入js文件。(ps:不要问为何不都用vue,设计如此),那么现在需要在vue中获取js控制弹框显隐的值,需要在js中获取vue中input后的内容。

实现: 先介绍一下MutationObserver

MutationObserver 是一个可以监听DOM结构变化的接口。

官方示例:

//选择一个需要观察的节点
var targetNode = document.getElementById('some-id');

// 设置observer的配置选项
var config = { attributes: true, childList: true, subtree: true };

// 当节点发生变化时的需要执行的函数
var callback = function(mutationsList, observer) {
    for(var mutation of mutationsList) {
        if (mutation.type == 'childList') {
            console.log('A child node has been added or removed.');
        }
        else if (mutation.type == 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
};

// 创建一个observer示例与回调函数相关联
var observer = new MutationObserver(callback);

//使用配置文件对目标节点进行观测
observer.observe(targetNode, config);

// 停止观测
observer.disconnect();

 那么根据案例代码,实现我们的vue  和 js demo

test.vue文件如下:

<template>
    <div>
        <div id="box111"></div>
        <div v-show="isShow">
            输入内容: <input type="text" name="fname" v-model='inputText'><br>
            <input type="submit" value="提交" @click="showTextHandler">
        </div>
        <div id="showText11">{{showText}}</div>
    </div>
</template>
<script>
  import init from '../utils/test'
  export default {
    name: 'index',
    data() {
      return {
        isShow: false,
        inputText: '',
        showText: ''
      }
    },
    mounted() {
      init()

      // 选择将观察突变的节点
      var targetNode = document.getElementById('showdiv');
      // 观察者的选项(要观察哪些突变)
      var config = {
        attributes: true,
        childList: true,
        subtree: true,
        characterData:true,
        characterDataOldValue:true,
        attributeOldValue:true
      };
      // 当观察到突变时执行的回调函数
      var callback = (mutationsList) => {
        mutationsList.forEach((item, index) => {
          console.log(item);
          if (item.type == 'childList') {
            console.log('获取到js变化:', item.target.innerHTML);
            this.isShow = item.target.innerHTML == 'show' ? true : false
          }
        });
      };
      // 创建一个链接到回调函数的观察者实例
      var observer = new MutationObserver(callback);
      // 开始观察已配置突变的目标节点
      observer.observe(targetNode, config);
    },
    methods: {
      showTextHandler() {
        document.getElementById('showText11').innerText = this.inputText//监听到的type类型是childList
        // this.showText = this.inputText  //这种方式监听到的type是characterData
      }
    }
  }
</script>

test.js文件如下:

export default function init() {
  let html = ''
  html += `<button id='btn11'>按钮</button>`
  html += `<div id='showdiv'></div>`
  document.getElementById('box111').innerHTML = html
  let btn = document.getElementById('btn11')
  let showdiv = document.getElementById('showdiv')

  btn.addEventListener('click', () => {
    console.log(showdiv.innerText);
    if(showdiv.innerText == 'show'){
      showdiv.innerText = 'hide'
    }else {
      showdiv.innerText = 'show'
    }
  })


  // 选择将观察突变的节点
  var targetNode1 = document.getElementById('showText11');
  // 观察者的选项(要观察哪些突变)
  var config1 = {
    attributes: true,
    childList: true,
    subtree: true,
    characterData:true,
    characterDataOldValue:true,
    attributeOldValue:true
  };
  // 当观察到突变时执行的回调函数
  var callback1 = (mutationsList) => {
    mutationsList.forEach((item, index) => {
      console.log(item);
      if(item.type =='childList'){
        console.log("获取到vue内容变化:",item.target.innerHTML);
      }
    });
  };
  // 创建一个链接到回调函数的观察者实例
  var observer1 = new MutationObserver(callback1);
  // 开始观察已配置突变的目标节点
  observer1.observe(targetNode1, config1);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值