2017-04-25 90 views
2

我想從我的html頁面中的java-script文件中調用一個函數。我的代碼如下所示。javascript函數在html中不起作用

的index.html

<!DOCTYPE html> 
<html><body> 
<h2>Web1</h2> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script src="js/main.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     myFunction1();  // call function 
}); 
</script> 

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script src="socket.io.js"></script> 
</body> 
</html> 

main.js

$(function() { 
function myFunction1() { 
    alert("ff"); 
} 

myFunction1()是我的職責。它在$(function() {}之內。但它不起作用。 它工作時,在main.js

function myFunction1() { // function declaration in global scope 
alert("ff"); 
} 

寫道如何main.js定義myFunction1()$(function() {}?請幫幫我?在最後一行})()

(function() { 
    function myFunction1() { 
     alert("ff"); 
    } 
})() 

注意的語法錯誤:

+0

我必須清除它。但我的問題不解決。 – joe

回答

2

試試這個。

+0

它不工作。 – joe

+0

試一下: '' – gaganshera

+0

您在控制檯中遇到的錯誤是什麼? – gaganshera

0

在我看來,它並不像是真的需要嵌套在匿名函數中。如果你刪除它應該很好。

0

您需要jquery以後使用它的一部分。

基本上你需要切換函數聲明和函數調用,因爲在jquery的onload裏面,你需要調用函數。

Puth全局範圍外的函數聲明。

function myFunction1() { // function declaration in global scope 
 
    alert("ff"); 
 
} 
 

 
$(function() { 
 
    myFunction1();  // call function 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h2>Web1</h2>

版本與本地函數聲明。

$(function() { 
 
    function myFunction1() { // local function declaration 
 
     alert("ff"); 
 
    } 
 

 
    myFunction1();   // call function 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h2>Web1</h2>

+0

它正在工作,但是我想調用js文件中的$(function(){}函數 – joe

+0

你叫它裏面,但也許我不明白你的問題,請添加一些信息 –

+0

在我的html iwant調用myfunction1()。該函數是在js文件中定義,它在$(function(){}。從函數$(function(){})中調用該函數,現在它不起作用了 – joe

0

嘗試調用你的函數裏面myFunction1()事件.ready()

當DOM完全加載它會執行。

<script type="text/javascript"> 
    $(document).ready(function(){  
     myFunction1(); 
    }); 

</script> 
+0

它不工作。 – joe

0

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<!-- 
 
You can uncomment this in your case 
 
<script type="text/javascript" src="js/main.js"></script> 
 
--> 
 
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> 
 
</head> 
 

 
<body> 
 
<h2>Web1</h2> 
 

 
<script> 
 
$(document).ready(function(){ 
 
myFunction1(); 
 
}); 
 
function myFunction1() { 
 
    alert("ff"); 
 
} 
 
</script> 
 
</body> 
 
</html>

這裏是你的答案。 讓我告知它對你有沒有幫助。謝謝!在最後一行})()

+0

我想在js文件中寫入js函數。 – joe

+0

是啊!你可以通過將腳本部分複製粘貼到單獨的js文件中,然後取消註釋腳本標記的註釋代碼來完成。 :) –

0

你缺少進口jQuery的和語法錯誤試試這個:

<!DOCTYPE html> 
<html><body> 
<h2>Web1</h2> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript" src="js/main.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){  
     myFunction1(); 
    }); 
function myFunction1() { 
    alert("ff"); 
} 
</script> 
</body> 
</html> 

OR

<!DOCTYPE html> 
<html><body> 
<h2>Web1</h2> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript" src="js/main.js"></script> 
<script type="text/javascript"> 
    $(function(){  
     myFunction1(); 
    }); 
function myFunction1() { 
    alert("ff"); 
} 
</script> 
</body> 
</html>