如何使用在另一个函数中声明的变量 [英] How to use a variable declared in another function

查看:160
本文介绍了如何使用在另一个函数中声明的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使全局变量为"lists",并在此函数上声明其值,以便也可以在另一个函数上使用它:

I want to make global a variable called "lists" and declare its value on this function so I can use it on another function as well:

   var lists;
   document.addEventListener("DOMContentLoaded", function(event) {
  var parentOfMyList = document.body;
    attributes: true,
    childList: true,
    subtree: true
  };

  var callback = function(mutationsList) {
    for (var mutation of mutationsList) {
      if (mutation.type == 'childList') {
        var elt = document.getElementById("contents");
        if (elt) {
          lists = elt.textContent;
          console.log(lists);
          observer.disconnect();
        }
      }
    }
  };
  var observer = new MutationObserver(callback);
  observer.observe(parentOfMyList, config);
});

另一个功能:

function playthis() {
 const input = lists;
 const sounds = input.toLowerCase();
 console.log(sounds);
}

我尝试通过在变量"lists"之前删除"var"来进行尝试,但是它没有用.它说输入"是不确定的.怎么了?

I tried by removing "var" before the variable "lists" but it didnt work. It says "input" is undefined. Whats wrong?

谢谢

推荐答案

您必须在Global Scope中定义Global Variable.我无法与last})联系;在您的第一个代码部分中.它不会告诉var列表是在某个代码块中还是在全局外部声明.如果您可以发布完整的代码,则可以很容易地看到其范围与所调用函数的关系.我的意思是将其放在$(document).ready(function() {之前.如果您显示了完整的代码,那么将很容易检测到.请尝试以下代码-

You must need to define Global Variable in Global Scope. I am not able to connect with last }); in your first code section. it does not tell whether var lists is in some code block or declared globally outside. if you can post complete code it will be easy to see its scope with respect to function you are calling it in. I mean Put it before $(document).ready(function() {. If you have shown complete code then it would be easy to detect. Please try below code -

//Globally Defined
var lists;

$(document).ready(function() {


   document.addEventListener("DOMContentLoaded", function(event) {
  var parentOfMyList = document.body;
    attributes: true,
    childList: true,
    subtree: true
  };

  var callback = function(mutationsList) {
    for (var mutation of mutationsList) {
      if (mutation.type == 'childList') {
        var elt = document.getElementById("contents");
        if (elt) {
          lists = elt.textContent;
          console.log(lists);
          observer.disconnect();
        }
      }
    }
  };
  var observer = new MutationObserver(callback);
  observer.observe(parentOfMyList, config);
});

function playthis() {
 const input = lists;
 const sounds = input.toLowerCase();
 console.log(sounds);
}

如果这不能解决您的问题,请在下面更改代码.我想在几个地方查看数据.

If this does not solve your problem please do below changes to code. I want to see data at few places.

var callback = function(mutationsList) {

    // In case mutationsList if JSON Object Otherwise just console mutationsList
    console.log(JSON.stringify(obj));
    for (var mutation of mutationsList) {
      if (mutation.type == 'childList') {
        var elt = document.getElementById("contents");
        console.log(elt);
        if (elt) {

          lists = elt.textContent;
          console.log(lists);
          observer.disconnect();
        }
      }
    }
};

这篇关于如何使用在另一个函数中声明的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆