本文介绍了从javascript调用Jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从javascript调用jQuery函数?

How can i call a jQuery function from javascript?

 //jquery
 $(function() {

        function my_fun(){
               /.. some operations ../
        }
 });

 //just js
 function js_fun () {

       my_fun(); //== call jquery function
 }


推荐答案

你不能。

function(){

    function my_fun(){
           /.. some operations ../
    }
}

这是关闭。 my_fun()仅在该匿名函数内定义。如果您在正确的范围级别(即全局范围内)声明它,则只能调用 my_fun()

That is a closure. my_fun() is defined only inside of that anonymous function. You can only call my_fun() if you declare it at the correct level of scope, i.e., globally.

$(function(){/ * something * /})是一个IIFE,意味着它在DOM准备就绪时立即执行。通过在该匿名函数中声明 my_fun(),可以防止脚本的其余部分看到它。

$(function () {/* something */}) is an IIFE, meaning it executes immediately when the DOM is ready. By declaring my_fun() inside of that anonymous function, you prevent the rest of the script from "seeing" it.

当然,如果你想在DOM完全加载时运行这个函数,你应该执行以下操作:

Of course, if you want to run this function when the DOM has fully loaded, you should do the following:

function my_fun(){
    /* some operations */
}

$(function(){
    my_fun(); //run my_fun() ondomready
});

// just js
function js_fun(){
   my_fun(); //== call my_fun() again
}

这篇关于从javascript调用Jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:05