如何从javascript调用jquery函数

how to call function in jquery from javascript

本文关键字:jquery 函数 调用 javascript      更新时间:2023-09-26
<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>

如何调用存在于jquery中的gotof()下面是在jsfiddle

上编写的代码

您的代码中有一些错误。修复它应该看起来像这样:

$.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://docs.jquery.com/Plugins/Authoring -这应该可以回答您的问题。你也没有正确定义你的功能;而不是

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

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