Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “如何调试”;未定义引号";Chrome工具中的错误?_Javascript_Html_Arrays - Fatal编程技术网

Javascript “如何调试”;未定义引号";Chrome工具中的错误?

Javascript “如何调试”;未定义引号";Chrome工具中的错误?,javascript,html,arrays,Javascript,Html,Arrays,我在Chrome工具中不断发现一个错误,即“引号未定义”。我能找到解决办法。当我点击Chrome工具向我显示错误时,它会出现在Math.floor-Math.random中 这是我的密码 <div class="container-fluid text-center"> <h1>Random quote Generator</h1> <br> <button class="btn btn-danger" type="submit">

我在Chrome工具中不断发现一个错误,即“引号未定义”。我能找到解决办法。当我点击Chrome工具向我显示错误时,它会出现在
Math.floor
-
Math.random

这是我的密码

<div class="container-fluid text-center">
<h1>Random quote Generator</h1>


<br>
<button class="btn btn-danger" type="submit">New Quote</button>

<a href="#" id="tweet" class="btn btn-primary">Tweet Out!</a>
<br>
<div class="quotes">
    <span class="quote"></span>
    <span class="author"></span>
</div>

</div>

$(document).ready(function(){

function getQuote(){
var quotes = ["With the new day comes new strength and new thoughts.", "War is 
peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
the advancement of knowledge and the dissemination of truth.", "Success is 
getting what you want. Happiness is wanting what you get." , "You know an odd 
feeling? Sitting on the toilet eating a chocolate candy bar."];
var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];
};

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);

  $(".btn").on("click", function(){
  getQuote();
  });

 });

随机报价生成器

新报价
$(文档).ready(函数(){ 函数getQuote(){ var quotes=[“新的一天带来了新的力量和新的思想。”,“战争就是这样。” 和平。自由是奴隶。无知是力量。”,“教育的目标是 知识的进步和真理的传播 得到你想要的。幸福就是想要你得到的 感觉如何?坐在马桶上吃巧克力糖; var author=[“-埃莉诺·罗斯福”,“乔治·奥威尔”,“约翰·F·肯尼迪”,“戴尔” 卡内基、乔治·卡林; }; var randumNum=Math.floor((Math.random()*quotes.length)); var randomQuote=引号[randomNum]; var randomAuthor=作者[randomNum]; $(“.quotes”).text(随机引用); $(“.author”).text(作者); $(“.btn”)。在(“单击”,函数(){ getQuote(); }); });
我认为您过早地结束了
getQuote
函数。您的代码应该是:

$(document).ready(function(){

function getQuote(){
var quotes = ["With the new day comes new strength and new thoughts.", "War is 
peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
the advancement of knowledge and the dissemination of truth.", "Success is 
getting what you want. Happiness is wanting what you get." , "You know an odd 
feeling? Sitting on the toilet eating a chocolate candy bar."];
var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);
};

  $(".btn").on("click", function(){
  getQuote();
  });

 });

quotes变量是在方法getQuotes()中定义的,因此在外部不可见。我假设您的getQuote函数关闭得太早,只需在设置author之后移动
}

$(document).ready(function(){
 function getQuote(){
    var quotes = ["With the new day comes new strength and new thoughts.", "War is 
             peace. Freedom is slavery. Ignorance is strength.", "The goal of education is 
             the advancement of knowledge and the dissemination of truth.", "Success is 
             getting what you want. Happiness is wanting what you get." , "You know an odd 
            feeling? Sitting on the toilet eating a chocolate candy bar."];

     var author = ["-Eleanor Roosevelt", "George Orwell", "John F. Kennedy", "Dale 
Carnegie", "George Carlin"];

  var randumNum = Math.floor((Math.random()*quotes.length));
  var randomQuote = quotes[randomNum];
  var randomAuthor = author[randomNum];

  $(".quotes").text(randomQuote);
  $(".author").text(randomAuthor);
 };


 $(".btn").on("click", function(){
   getQuote();
  });
 });

getQuote函数仅关闭在函数外部定义2个veriables“quotes”和“author”的函数。您试图读取其值,但这是不可能的,因此,如果没有定义引号,则需要在这行代码之后关闭函数(};)

$(".author").text(randomAuthor);
$(文档).ready(函数(){
函数getQuote(){
var引用=[“新的一天带来新的力量和新的思想。”,“战争是和平,自由是奴役,无知是力量。”,“教育的目标是知识的进步和真理的传播。”,“成功是得到你想要的。幸福是想要你得到的。”,“你知道一种奇怪的感觉吗?坐在马桶上吃巧克力糖。”];
var作者=[“-埃莉诺·罗斯福”、“乔治·奥威尔”、“约翰·F·肯尼迪”、“戴尔·卡内基”、“乔治·卡林”];
var randomNum=Math.floor((Math.random()*quotes.length));
var randomQuote=引号[randomNum];
var randomAuthor=作者[randomNum];
$(“.quotes”).text(随机引用);
$(“.author”).text(作者);
};
$(“.btn”)。在(“单击”,函数(){
getQuote();
});
});

点击我看看


您已声明
quotes
在函数内部
getQuote()
并在函数外部使用它搜索变量范围。感谢您的反馈…我已经完成了您提到的操作,现在我在右括号旁边看到一个红色的“x”,表示我缺少a”)“符号。当尝试在中添加一个时,返回引号时会出现一个错误。如果有效,除非您忘记在作者之后删除上一个close。错误不在代码段上,因为上面的注释显示了按照我打算的方式修改的代码,并且在空白javascript文件上工作正常,您的问题在代码中的另一行,当您修复此问题时,会触发此问题。请查看我在本帖编辑版本中随附的实时演示。谢谢您的反馈。我将您的代码复制并粘贴到我的编辑器中,出现一个错误,指出我缺少“)”项,在结束$(“.author”).text(randomAuthor);。。。当我添加大括号时,它返回给我关于引号的错误。