【问题标题】:Accessing a Declared Variable访问声明的变量
【发布时间】:2021-05-10 22:24:53
【问题描述】:

好时光。如何访问已声明的变量?我这样声明:

$ (document) .ready (function () {

    var fotorama = $fotoramaDiv.data ('fotorama');

$ (document) .on ('click', '.btn-del-model', function (e) {

console.log (e);
console.log (fotorama); // Uncaught ReferenceError: fotorama is not defined

   });

});

带有“.btn-del-model”类的按钮是动态创建的。告诉我如何访问“fotorama”变量?

【问题讨论】:

  • $(fotoramaDiv).data ('fotorama')你试过这个吗?
  • $(document).on('click', '.btn-del-model', function(e) { console.log($(fotoramaDiv).data('fotorama')); //Uncaught ReferenceError: fotoramaDiv is not defined });
  • fotoramaDiv 你需要从 DOM 中获取它。使用类语法或 id。如果你能在这里分享你所有的 HTML 代码会更好
  • 成功了!谢谢!漫不经心地写了变量。 8)
  • 我已将此信息添加为答案。您可以将答案标记为已接受的解决方案。因为它也可以帮助其他人。

标签: javascript variables document-ready defined fotorama


【解决方案1】:

您需要使用类语法或 id 从 DOM 中为 fotoramaDiv 赋值。

// for example
const fotoramaDiv = document.querySelector('.yourClassName');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多