JavaScript和Jquery:如何从JavaScript函数中调用Jquery函数

JavaScript和Jquery:如何从JavaScript函数中调用Jquery函数,javascript,Javascript,我有一个按钮,当点击它时,它会向javascript发送一个变量 功能。当变量等于“link”时,我想调用一个名为makeLink()的jquery函数 这就是我所拥有的: function getText(change) { if(change == "link") { //call jquery function called makeLink() } } 下面是我的jquery函数,它创建了一个带有表单的模式弹出窗口: $(document).ready

我有一个按钮,当点击它时,它会向javascript发送一个变量 功能。当变量等于“link”时,我想调用一个名为makeLink()的jquery函数

这就是我所拥有的:

function getText(change)
{
   if(change == "link")
   {
      //call jquery function called makeLink()

   }

}
下面是我的jquery函数,它创建了一个带有表单的模式弹出窗口:

$(document).ready(function(){

function makeLink() {
    if ($("#makeALinkModalPopup").is(":hidden")){
    $("#makeALinkModalPopup").fadeIn("slow");

     $("#backgroundPopup").css({  
        "height": document.documentElement.offsetHeight

      });

    $("#backgroundPopup").css({"opacity": "0.7"});
    $("#backgroundPopup").fadeIn("slow"); 

        }

    }


});

感谢您的帮助。

您不应该在document ready事件中定义函数,而应该在单独的文件中定义函数

那么,你有:

//call jquery function called makeLink()

makeLink()

不应在document ready事件中定义函数,而应在单独的文件中定义

那么,你有:

//call jquery function called makeLink()

makeLink()

将makeLink移动到全局范围并正常调用它。只有JavaScript函数。您看到的区别仅限于范围


正如其他人所说,移除文档。准备好包装。您的函数不需要在这里定义,因为它无法在document.ready之外看到。

将makeLink移动到全局范围并正常调用它。只有JavaScript函数。您看到的区别仅限于范围


正如其他人所说,移除文档。准备好包装。您的函数不需要在此处定义,因为它无法在文档外部看到。就绪。

删除文档。就绪包装可使makeLink对页面的其余部分可用

    function getText(change){
      if(change == "link") {
      //call jquery function 

        makeLink()

      }
    }


    function makeLink() {
      if ($("#makeALinkModalPopup").is(":hidden")){
        $("#makeALinkModalPopup").fadeIn("slow");

        $("#backgroundPopup").css({  
          "height": document.documentElement.offsetHeight

        });

        $("#backgroundPopup").css({"opacity": "0.7"});
        $("#backgroundPopup").fadeIn("slow"); 

      }
    }

删除document.ready包装以使makeLink可用于页面的其余部分

    function getText(change){
      if(change == "link") {
      //call jquery function 

        makeLink()

      }
    }


    function makeLink() {
      if ($("#makeALinkModalPopup").is(":hidden")){
        $("#makeALinkModalPopup").fadeIn("slow");

        $("#backgroundPopup").css({  
          "height": document.documentElement.offsetHeight

        });

        $("#backgroundPopup").css({"opacity": "0.7"});
        $("#backgroundPopup").fadeIn("slow"); 

      }
    }

你不需要准备好dom

只要

function makeLink() {
    if ($("#makeALinkModalPopup").is(":hidden")){
    $("#makeALinkModalPopup").fadeIn("slow");

     $("#backgroundPopup").css({  
        "height": document.documentElement.offsetHeight

      });

    $("#backgroundPopup").css({"opacity": "0.7"});
    $("#backgroundPopup").fadeIn("slow"); 

        }

    }
只要

function getText(change)
{
   if(change == "link")
   {
      makeLink();

   }

}
如果您想在dom ready上使用该函数,那么您需要这样做

$(文档).ready(makeLink)<我在sytax可能是错的,但为了安全起见,我知道这是可行的

$(document.ready(function(){
// do what ever you want
//even call make link

makeLink();
}

你不需要准备好dom

只要

function makeLink() {
    if ($("#makeALinkModalPopup").is(":hidden")){
    $("#makeALinkModalPopup").fadeIn("slow");

     $("#backgroundPopup").css({  
        "height": document.documentElement.offsetHeight

      });

    $("#backgroundPopup").css({"opacity": "0.7"});
    $("#backgroundPopup").fadeIn("slow"); 

        }

    }
只要

function getText(change)
{
   if(change == "link")
   {
      makeLink();

   }

}
如果您想在dom ready上使用该函数,那么您需要这样做

$(文档).ready(makeLink)<我在sytax可能是错的,但为了安全起见,我知道这是可行的

$(document.ready(function(){
// do what ever you want
//even call make link

makeLink();
}

该函数不是全局函数-这就是您不能调用它的原因。它与jQuery无关,只是它恰好是一个包含它的jQuery“ready”处理程序。该函数不是全局函数——这就是您不能调用它的原因。它与jQuery无关,只是它恰好是一个包含它的jQuery“ready”处理程序。你能给我更多的细节吗?或者是一个例子。非常感谢你!你能告诉我更多的细节吗?或者是一个例子。非常感谢你!谢谢你的帮助。我一定是做错了什么,因为删除document.ready wrapping没有任何作用。我会继续研究。谢谢。控制台里有消息吗?你能为我们制作一个jsfiddle.net吗?谢谢你的帮助。我一定是做错了什么,因为删除document.ready wrapping没有任何作用。我会继续研究。谢谢。控制台里有消息吗?你能为我们制作一个jsfiddle.net吗?