【问题标题】:Setting a reactive variable from inside a jquery click event block从 jquery click 事件块中设置反应变量
【发布时间】:2021-10-11 03:56:45
【问题描述】:

我一直在使用 jquery 执行一些 dom 操作任务,我需要从 click 事件块中更新我的反应变量。

我有这个反应变量

 data() {
  return {
inside_image:'' 
   };
  },
  mounted() {
  $("img").click(function() {
  var image_string = $(this).attr('src');
  this.inside_image = 'https://google.com/'+image_string;
});

我需要更新但我无法访问我的数据响应变量,很可能this 没有指向 vue 数据。有没有一种方法可以让我从点击事件块内部访问我的反应变量?这个块

$("img").click(function() {
  var image_string = $(this).attr('src');
 //This is currently not updating my reactive vars
  this.inside_image = 'https://google.com/'+image_string;
});

【问题讨论】:

    标签: jquery vue.js


    【解决方案1】:

    我将this 的引用保存在块外并工作

      var vue_this = this 
      $("img").click(function() {
      var image_string = $(this).attr('src');
      //alert(image_string);
      vue_this.inside_image = 'https://google.com/'+image_string;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 2020-11-24
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多