如何将点击中定义的变量传递给函数?

时间:2018-02-02 03:44:59

标签: javascript jquery

我正在尝试将变量mySearchTitle传递给函数以便阅读page: mySearchTitle

   var mySearchTitle;
    $("#wikiResults li a").on("click", function(e) {
      e.preventDefault();
      mySearchTitle = $(this).html().replace(/ /g,"_");
      console.log($(this).html().replace(/ /g,"_"));
      textWiki(mySearchTitle);
    });
  }
  function textWiki() {
    $.getJSON("https://"+ queryLang +".wikipedia.org/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text&limit=1&callback=?", {
      page: mySearchTitle

但我进入控制台

Uncaught ReferenceError: mySearchTitle is not defined

1 个答案:

答案 0 :(得分:0)

要在函数中使用变量,您需要将其作为参数传递给函数

function textWiki(mySearchTitle){
         //use mySearchTitle as needed
}