2012-02-18 69 views
2

如果我有這樣的代碼在一個名爲custom.js:jQuery的 - 調用外部的js文件的功能

var kennel = function(){this._init();}; 

kennel.prototype = { 
    _init: function() { 
     this.setListeners(); 
    }, 
    setListeners: function(){ 
     ... 
    }, 
    getCats: function(){ 
     alert("Get cats"); 
    } 
}; 

如何調用getCats()一些任意的HTML文件?

回答

3

首先您將包括使用<script>標籤,一般在<head>的JavaScript,然後之後你可以創建另一個<script>標籤,你可以將自己的JavaScript代碼在特定頁面,而這使用由包含文件提供的功能。例如:

<head> 
    <script type="text/javascript" src="path/to/custom.js"></script> 
    <script type="text/javascript"> 
     var myKennel = new kennel(); 
     myKennel.getCats(); // alerts "Get cats" 
    </script> 
</head> 

而且,你已經發布的代碼無關的jQuery - 它就是普通的,香草的JavaScript。

相關問題