2011-12-16 21 views
3
<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> 

我如何調用gotof()存在於jQuery的 及以下調用函數jQuery是寫在jsfiddle如何從javascript

+3

你的語法是有缺陷的。無論如何,如果你想定期調用`gotof()`,爲什麼還要`$ .fn`?這裏我沒有看到jQuery的必要性。 – pimvdb 2011-12-16 11:21:30

回答

8

有代碼中的一些錯誤。固定它應該是這樣的:

$.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/

0

退房http://docs.jquery.com/Plugins/Authoring代碼 - 這應該回答你questior。你也沒有正確定義你的功能;而不是

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

你需要

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