JavaScript和Jquery:如何在javascript函数中调用jquery函数

时间:2012-08-02 21:05:58

标签: javascript

我有一个表单中的按钮,当单击时将变量发送到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"); 

        }

    }


});

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

删除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"); 

      }
    }

答案 1 :(得分:1)

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

Read about scope here.

正如其他人所说,删除document.ready包装。你的函数不需要在那里定义,因为它不能在document.ready之外看到。

答案 2 :(得分:0)

您不应在文档就绪事件中定义函数,而应在单独的文件中定义。

然后,你在哪里:

//call jquery function called makeLink()

刚刚放

makeLink()

答案 3 :(得分:0)

你不需要准备好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上使用该功能,那么您需要这样做。

$(document).ready(makeLink);<我可能在sytax中错了,但为了安全,我知道这有效..

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

makeLink();
}