如何使用在另一个函数

时间:2018-05-13 21:36:52

标签: jquery global-variables

您好,

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

   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);
}

我尝试删除" var"在变量"列出"之前但它没有用。它说"输入"未定义。怎么了?

谢谢

2 个答案:

答案 0 :(得分:-1)

你为什么要做Pod::Spec.new do |s| s.name = 'JCore' s.version = '0.2.3' s.summary = 'This is an UI Lib for iOS' s.platform = :ios, "10.0" s.description = <<-DESC This is an UI Lib to iOS. This description is too large than summary DESC s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'Josue Hernandez' => 'example@gmail.com' } s.source = { :git => 'https://github.com/example/JCore.git', :tag => s.version.to_s } s.source_files = 'LibraryComponents/**/*' s.resource_bundles = { 'JCore' => ['LibraryComponents/Resources/*.xcassets','LibraryComponents/Resources/**/*.json'] } ?你不能只const input = lists;。如果变量不是全局变量,则会在const sounds = lists.toLowerCase();上出现错误。你确定变量是一个字符串吗?尝试在const input = lists;

的顶部添加console.log(typeof lists)

答案 1 :(得分:-1)

您必须在全局范围中定义全局变量。我无法连接上一个});在你的第一个代码部分。它不会告诉var列表是在某个代码块中还是在全局范围内声明。如果你可以发布完整的代码,那么就可以很容易地看到你调用它的函数的范围。我的意思是把它放在$(document).ready(function() {之前。如果您已经显示完整的代码,那么很容易检测到。请尝试以下代码 -

//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);
}

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

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();
        }
      }
    }
};
相关问题