简体   繁体   English

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

[英]JavaScript and Jquery: How can you call a jquery function from within a javascript function

I have a button in a form that when clicked sends a variable to a javascript function. 我有一个按钮,单击后会将变量发送到javascript函数。 When the variable equals "link" I want to call a jquery function called makeLink(). 当变量等于“ link”时,我想调用一个名为makeLink()的jQuery函数。

This is what I have: 这就是我所拥有的:

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

   }

}

And here is my jquery function that creates a modal pop up with a form: 这是我的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"); 

        }

    }


});

Thanks for your help. 谢谢你的帮助。

Remove the document.ready wrapping to make makeLink available to the rest of the page 删除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"); 

      }
    }

Move makeLink to the global scope and call it normally. 将makeLink移到全局范围并正常调用。 There are only JavaScript functions. 只有JavaScript函数。 The distinction you are seeing is scope only. 您所看到的区别仅是范围。

Read about scope here. 在此处了解范围。

As someone else said, remove the document.ready wrapping. 就像其他人所说的,请取出文档。准备好包装。 Your function need not be defined there, because it cannot be seen outside of document.ready. 您的函数无需在此处定义,因为无法在document.ready之外看到它。

You shouldn't define the function inside the document ready event, but on a separate file. 您不应在文档准备事件中定义函数,而应在单独的文件中定义。

Then, where you have: 然后,您在哪里:

//call jquery function called makeLink()

Just put 刚放

makeLink()

You dont need the dom ready. 您不需要准备好dom。

Just have 刚有

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

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

      });

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

        }

    }

Just have 刚有

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

   }

}

If you wanted to use the function on the dom ready then you would need to do. 如果您想在dom上使用该功能,则需要这样做。

$(document).ready(makeLink); < I might be wrong in sytax but to be safe i know this works.. <我在语法上可能是错误的,但是为了安全起见,我知道这是可行的。

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

makeLink();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM