Javascript 如何从javascript调用jquery中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8533439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:24:19  来源:igfitidea点击:

how to call function in jquery from javascript

javascriptjqueryfunction

提问by Rafee

<script type="text/javascript" src="jscripts/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function(){
alert("funciton");
    $(function(){
        $.fn.gotof(){
            alert("I am calling form jquery");
        }           
    });
});
</script>


<input type="button" onclick="dofunc();">

<script type="text/javascript">
    function dofunc(){
        gotof();
    }
</script>

how do i call gotof()that is present in jquery and below is the code written over jsfiddle

我如何调用gotof()jquery 中存在的代码,下面是通过jsfiddle编写的代码

回答by Richard Dalton

There are a few errors in your code. Fixed it should look like this:

您的代码中有一些错误。固定它应该是这样的:

$.fn.gotof = function() { // has to be defined as a function, does not need to be inside a nested document ready function
    alert("I am calling form jquery");
};

$(document).ready(function() {
    alert("function is ready to use now");
});

function dofunc() {
    $.fn.gotof();  // can call it using $.fn.gotof(), but it should really be called properly via a selector $('div').gotof();
}

http://jsfiddle.net/pSJL4/8/

http://jsfiddle.net/pSJL4/8/

回答by Graham

Check out http://docs.jquery.com/Plugins/Authoring- this should answer your questior. You also are not defining your function correctly; instead of

查看http://docs.jquery.com/Plugins/Authoring- 这应该可以回答您的问题。您也没有正确定义您的功能;代替

$.fn.gotof(){
            alert("I am calling form jquery");
        }

you need

你需要

$.fn.gotof = function(){
            alert("I am calling from jquery");
        };